Forum Moderators: coopster & phranque

Message Too Old, No Replies

calling a subroutine in another script

         

dreaming of nascar

6:20 pm on Apr 1, 2003 (gmt 0)

10+ Year Member



I have a subroutine that I am using for multiple PERL scripts and was wondering if there was a way that I could put this script into it's own file and then call it from any other script that I want. I tried using "use" and "require" but was unsuccessful. The file that has the subroutine in it has nothing else. The file looks like this:

#!/usr/bin/perl

sub blah{
code
code
code
code
code
}

Then in another script I would like to be able to call that subroutine. Do I need more in the file with the subroutine or what do I need to do to accomplish this?

Thanks

D O N

andreasfriedrich

6:36 pm on Apr 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



do [perldoc.com], require [perldoc.com], and use [perldoc.com] are used to access code from some eternal library.

do [perldoc.com] EXPR uses the value of EXPR as a filename and executes the contents of the file as a Perl [perl.com] script. Its primary use is to include subroutines from a Perl [perl.com] subroutine library.

In Perl [perl.com] 5 it is more common, however, to use [perldoc.com] a module. Just create a file named MyModule.pm and then

[url=http://www.perldoc.com/perl5.8.0/pod/func/use.html]use[/url] MyModule;
in your code.

Andreas