SPUG: Scoping Guidelines; comments?

Bill Alford billa at willapabay.org
Fri Feb 18 13:04:44 CST 2000


This is similar to my technique too.  When the program starts getting big,
or I realize it's going to be longer lived and edited by others, I do the
following:

1. Add:

use package;  # put all my packages in
use strict;

# declare globals with my
...

main();
exit();

2. wrap everything in sub main {...}

3. Proceed in development like it's a very picky language.  Try to avoid
code that only people that are very used to perl use (like the implicit
variables and side effects).  Always use 'my' instead of 'local'.

If I do use something tricky, comment like a mad man (partly because I
know I am lazy and avoid remembering things that are in manuals, and
partly for when someone takes it over and I'm not around).


I can imagine cases where you want to temorarily hid the current value of
a global variable and call subroutines with the global value changed, but
you can always get around that using other techniques that won't confuse
someone not familiar with the behavior of dynamic scoping (Lisp like).  
Lexical scoping is a lot easier to understand for beginners (the draw a
"fence" with the curly braces approach :) .  IMHO, it also leads to more
maintanable code.  I don't know about you, but I don't want to be called
six months after leaving a job with questions about how a program works.
:)

But as usual, techiques used are a question of style and the "company"
culture where you are at. 

Bill

On Fri, 18 Feb 2000, jimfl wrote:

> --Quoth Phillip Neal <phillipneal at hotmail.com> On Friday, February 18, 2000 
> 8:04 AM +0000:
> 
>      > My rules of thumb have always been:
>      >
>      > 1. Suspect everything is global
> 
>   My, my, my.
> 
>   At some point when a perl program gets to be a certain size I
> 
>       use strict;
>       use vars qw($foo $bar %baz);
> 
>   I also am generally in the habit of doing stuff like
> 
>       foreach my $thing in (@stuff) {
>         my $tmp = munge($thing);
>       }
> 
>   I generally only use 'local' when I have to, i.e.:
> 
>       sub spit_it_out {
>         local *FILEHANDLE = shift;
>         my @stuff = @_;
>         print FILEHANDLE join("\n", @stuff);
>       }
> 
>   or
> 
>     {
>       local $SIG{__DIE__} = \&dont_die;
>       do_something_deadly;
>     }
> 
>   My guideline would be use lexical scoping unless you understand why you
>   would need to use dynamic scoping, which boils down, for beginners, to
>   "Never, ever use 'local'." Don Knuth has a good philosophy: In the
>   earlier chapters I'm going to make statements which, by the time you get
>   to later chapters you will recognise as being blatantly false.
> 
> --
> Jim Flanagan          Collective Technologies
> jimfl at colltech.com   http://www.colltech.com
> 
>  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>     POST TO: spug-list at pm.org        PROBLEMS: owner-spug-list at pm.org
>  Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
>  SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
>         Email to majordomo at pm.org: ACTION spug-list your_address
> 
> 


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    POST TO: spug-list at pm.org        PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
        Email to majordomo at pm.org: ACTION spug-list your_address





More information about the spug-list mailing list