SPUG: Calling a file as a sub-routine?
Matt Tucker
tuck at whistlingfish.net
Thu Jul 12 15:00:22 CDT 2001
-- Jay Scherrer <jay at Scherrer.com> spake thusly:
> How can I call a separate .pl file into the main as a sub-routine?
> I am trying to create separate reusable files instead of listing all
> of the routines as subs in a large program.
>
> How do I set up a separate file called outside.pl and then use it as a
> subroutine inside the main program called mainProgram.pl?
> example:
> outside.pl
> sub outside {
> do something;
> }
>
> mainProgram.pl
># !/usr/bin/perl -w
> require "outside.pl";
> if ($foo = $bar) {
> outside.pl;
> }
Why not just:
if ($foo == $bar) {
outside();
}
And if you're going to start doing that sort of thing, why not use
packages?
Outside.pm:
package Outside;
sub outside {
do something;
}
main.pl:
#!/usr/bin/perl
use Outside;
if ($foo == $bar) {
Outside::outside(); # Or use Exporter if you don't want to
# be calling by package name every time
}
This is what the package system is for, and everyone's used to it.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 232 bytes
Desc: not available
Url : http://mail.pm.org/archives/spug-list/attachments/20010712/2cb5b9a7/attachment.bin
More information about the spug-list
mailing list