SPUG: Scoping Guidelines; comments?

Stuart Poulin stuart_poulin at yahoo.com
Fri Feb 18 13:04:25 CST 2000


I think it was on this list that I saw you can use:
   perl -MO=Lint,all script.pl
to catch implied usage of $_
   perldoc B::Lint


--- Tim Maher/CONSULTIX <tim at consultix-inc.com> wrote:
> SPUG-ticians,
> 
> The less experienced students in our Perl Programming classes tend to
> have lots of trouble dealing with my vs. local, and hate nasty surprises
> like File::Find trashing the $_ that's acting as their loop variable.
> 
> So I've been trying to come up with a "Cookbook" approach to 
> providing "Scoping Guidance", and would be interested in the comments
> of you professional Perl programmers out there on what I've come up
> with so far.  Have I left anything important out?  Have I made
> recommendations that you disagree with?
> 
> There's at least one recommendation here that I've never seen in any
> Perl documentation; enclosing your main program in {}.  This either
> means I've been amazingly clever in figuring out this useful technique 
> n my own, or else I'm stupid because everybody else achieves the same
> result in some other way; which is it? 8-}
> 
> There's also a sample program to drive home some of the points, which
> must be somewhat cryptic to fit on the same page.
> 
> TIA,
> 
> -Tim
> 
> 	The Consultix "Perl Scoping Guidelines", v0.8
> 
> FOR SMALL OR SIMPLE PROGRAMS
>   Relax and enjoy the freedom of Perl !
> 
> FOR ALL NON-TRIVIAL PROGRAMS (including LARGE/COMPLEX ones)
> * limit scope of variables
>   > use my for user variables, local for pre-defineds ($_, $/, etc.)
> * pass data to subs via the argument list (i.e., avoid globals)
> * enclose main program in { }
>   > so my vars won't have file scope, and leak into subs
> 	  defined below
> 
> IN LARGE OR COMPLEX PROGRAMS
> * avoid using $_ ; widespread use creates many problems!
> * localize temporary changes to built-in vars with new block
> 	{ local $,=' '; print; }
> * put "use strict" at top of program, to force use of my()
>   > if you need a global variable, qualify with package name; 
> 	$count becomes $main::count
> 
> SPECIAL CASES
> * $_ automatically localized to { } when provided as default
> 	loop or function variable
> *  foreach (LIST) { },  grep { } LIST,  map { }	LIST,  etc.
> 
> 				EXAMPLE
> 	(somewhat nonsensical, but fits on page with above!)
> 
> { 	# these parens confine MAIN variables declared with my()
> 	my  @names=('Joe', 'Nancy');
> 	local  $_='(303) 456-7890';
> 	foreach (@names) { print  "$_\n"; }  # different, local $_
> 	print  "$_\n";		# print the phone number
> 	my  $result = compute(@names);
> 	{ $\='\cL'; print  $result; }
> }	# end of MAIN
> 
> sub  compute  {
> 	local  $_='Test Data';	# won't collide with main's $_
> 	foreach my $var (@_) {	#  $var unavailable to s2()
> 		s2( )  and  print  $var ;   }	#  $_ available to s2()
> 	my  $result=s2();  return  $result;	# not main's $result
> }
> 
> *========================================================================*
> | Tim Maher, PhD  Consultix &		   (206) 781-UNIX/8649		 |
> |  Pacific Software Gurus, Inc		   Email: tim at consultix-inc.com	 |
> |  UNIX/Linux & Perl Training		   http://www.consultix-inc.com	 |
> | 2/22: UNIX  2/28: Perl Modules  2/29: Int. Perl  3/3: Pattern Matching |
> *========================================================================*
> 
>  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>     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
> 
> 
> 
__________________________________________________
Do You Yahoo!?
Talk to your friends online with Yahoo! Messenger.
http://im.yahoo.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





More information about the spug-list mailing list