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

Re: [MacPerl] where to put subroutines



At 05:00 -0700 4/13/99, Bruce Van Allen
> 1. Put subroutines in the same file as the script that calls them.
>...
> it. Thus it doesn't matter where in the file you put the subs (but not
> before '__END__').

Don't you mean "Not after __END__"?  The area after __END__ is data space.

> Most of us put the main part of the script first,
> followed by the subroutines.

Until recently... Recently I've started to declare the subs at the top.
Before any use or require statements or variables or....  As, e.g. :

   sub foo {};

I got burned on Lincoln Stein's excellent guestbook.cgi example (uses
CGI.pm). Between Lincoln's writing the code and the time I picked it up,
the Perl gods saw fit to clobber (er, "enhance") the namespace by adding a
lock() function as a Perl builtin.  [Grumble #%!^&~!]  Pre-declaring
subroutines (or defning them up top) overrides this sort of "surprise". I'm
of the school that says "code first, subroutines follow" so the happy
medium was declarations at the top, definitions at the bottom.

>
> 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

  3. Create a library and invoke it at the start of your script with
'require'.

I realize that libraries are de'classe these days but they are _easier_ to
write than modules :-)

> 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.

yeah, some of us still do it this way :-). Actually, in defense, I can say
that every time I paste one in I have to look at it. That keeps me thinking
about how it works and whether it needs tweaking (for this script) or
modifying (for general use).  So my functions keep improving. If I thought
of them as being in a black box, I don't think they'd get that benefit as
often.

- Vicki
---
      |\      _,,,---,,_       Vicki Brown <vlb@cfcl.com>
ZZZzz /,`.-'`'    -.  ;-;;,_   Journeyman Sourceror: Scripts & Philtres
     |,4-  ) )-,_. ,\ (  `'-'  P.O. Box 1269  San Bruno  CA  94066
    '---''(_/--'  `-'\_) http://www.cfcl.com/~vlb  http://www.macperl.org

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