Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

same sub used in multiple programs

Status
Not open for further replies.

mountainbiker

Programmer
Aug 21, 2002
122
GB
if i have a subroutines used in multiple programs, what is the best way to include these into programs (rather than maintain multiple copies of the same subroutines)?
 
Put it into a package (separate file with .pm suffix).
Then "use" the package wherever you need it.

-- foo.pm
package foo;
sub doSomething {
print "Hello";
}

-- elsewhere --
use foo;
foo->doSomething;

-- see PerlToot for more info on packaging and objects

 
I also tend toward the object oriented treatment, but
this also works in simple situations.

If you have your sub in a file named require_this.pl
and it looked like this,
Code:
#!/usr/local/bin/perl
sub doSomething {
print "Ran doSomething\n";
return(1);
}
1;

you could 'require' it in other programs like this,
Code:
#!/usr/local/bin/perl
require './require_this.pl';
unless (&doSomething) { die "Failed require\n"; }
'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top