P is for Practical | |
PerlMonks |
Re: Include subs from different perl fileby TGI (Parson) |
on Jun 06, 2007 at 19:39 UTC ( [id://619656]=note: print w/replies, xml ) | Need Help?? |
Code reuse is a wonderful thing. You are getting great advice here. Use modules (see perlmod) and avoid globals, pass data to subs as arguments. If you must use globals, you can either use package globals or import your variables so that you can see them in your main script. I'll show you how to specify the package of variables and subroutines here. Exporting/importing symbols is a bit more complex, so I'll leave that to the proper documentation. File Foo.pm:
File myscript.pl:
Also, ditch the unneeded & sigil from your subroutine calls. It can have unexpected consequences. Here are the relevent bits from perlsub, with emphasis added: ...If a subroutine is called using the & form, the argument list is optional, and if omitted, no @_ array is set up for the subroutine: the @_ array at the time of the call is visible to subroutine instead. This is an efficiency mechanism that new users may wish to avoid. See perlsub for more details. I think I need to put together a meditation - Ampersand Considered Harmful...
In Section
Seekers of Perl Wisdom
|
|