Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 10th, 2001, 05:40 PM
lorsul lorsul is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2001
Location: Pittsburgh
Posts: 32 lorsul User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
subroutines

Hi everyone!

I've created a fake case statement in one of my cgi pages -- it is quite long. This case statement has to be inputted into 35 more cgi pages.

I am new to perl and I know that you can create a sub routine within a program and call that subroutine from anywhere in the program.

Can you call a subroutine from another script?????? Enter the code into a subroutine in one script and call it in the remaining 35 scripts????

__________________
*******
Lorraine

Reply With Quote
  #2  
Old September 10th, 2001, 06:01 PM
Acid Reign Acid Reign is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2001
Posts: 190 Acid Reign User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 57 m 52 sec
Reputation Power: 8
Send a message via ICQ to Acid Reign
Easiest way to do this is by doing this:

First, the code that is repeated.
Code:
#!c:/perl/bin/perl

sub repeatedcode {
...repeated code goes here...
}

Then save it as something like repeat.sub (keeping it as the same place as your other scripts - keeps things simple)

Then to use it from your scripts, do this:
Code:
#!c:/perl/bin/perl

require 'repeat.sub';

&repeatedcode();


Obviously, the #!c:/perl/bin/perl needs to be changed to the correct line for your system.

Reply With Quote
  #3  
Old September 12th, 2001, 10:11 AM
ishnid's Avatar
ishnid ishnid is online now
kill 9, $$;
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Sep 2001
Location: Dublin, Eire
Posts: 5,697 ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 6 Days 22 h 38 m 28 sec
Reputation Power: 1507
You don't need a shebang line (i.e. #!/c:/perl/bin/perl) in your .sub file but you do have to make sure that it returns true at the end, if you're requiring it, otherwise your script will fail.

~ishnid

Reply With Quote
  #4  
Old September 12th, 2001, 10:36 AM
lorsul lorsul is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2001
Location: Pittsburgh
Posts: 32 lorsul User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
subroutines

i am using 'notepad' to write my scripts. when i try to save the repeated code as repeat.sub -- it saves it as repeat.sub.txt???

what does '.sub' mean???

when i tried saving it as repeat.pl and then using

require 'repeat.pl';

&repeatedcode();

My cgi page comes up but my repeated code does not.

do you have any idea why???

Reply With Quote
  #5  
Old September 12th, 2001, 11:37 AM
lorsul lorsul is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2001
Location: Pittsburgh
Posts: 32 lorsul User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Wink

i solved it -- THANKS FOR YOUR HELP

I saved it as repeat.pl

Using '1;' at the end of repeat.pl helped - thanks!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > subroutines


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT