|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
||||
|
||||
|
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 |
|
#4
|
|||
|
|||
|
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??? |
|
#5
|
|||
|
|||
|
i solved it -- THANKS FOR YOUR HELP
I saved it as repeat.pl Using '1;' at the end of repeat.pl helped - thanks! |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > subroutines |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|