[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] where to put subroutines



Jeffrey Hull <jwhull@drhull.com> wrote:
>Stupid of me.  I am trying to use some subroutines and can't figure out
>where to put them to allow MacPerl to find them - get Undefined Subroutine
>error mssg.

Not stupid, just a learning process. You have two choices:

1. Put subroutines in the same file as the script that calls them. Non-tech
explanation: When Perl gets the script, it reads the whole thing and
figures out what to do with its parts, and THEN does what the script tells
it. Thus it doesn't matter where in the file you put the subs (but not
before '__END__'). Most of us put the main part of the script first,
followed by the subroutines.

2. Create a module, and evoke it at the start of your script with the 'use'
function. With 'use' you're importing the module and its capabilities to
your script. There's much more to this than I'm saying here; most
programmers start out with subs in the same file, and only when the overall
script has matured, think about making a module (and modules are often
composed of various subs).

A handy thing to do if you've written a subroutine that you expect to
re-use is to keep a collection of your subs in a file, not to evoke from a
script, but just to have available to copy and paste into your latest
script. Put some #comments there to remind yourself of each sub's usage and
arguments.

Good luck!
- Bruce

~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Bruce Van Allen
bva@cruzio.com
831/429-1688
P.O. Box 839
Santa Cruz, CA  95061

===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org