SPUG: Scoping Guidelines; comments?

Rick Brannan rick at libronix.com
Fri Feb 18 11:33:33 CST 2000


A few years back, Tom Christiansen (who else?) posted a decent expository
tome on the subject to comp.lang.perl.misc. I just searched deja.com for it,
and found it. The link (hopefully it'll work) is:

http://x46.deja.com/[ST_rn=if]/getdoc.xp?AN=295246159&CONTEXT=950895037.2147
352618&hitnum=1

If the link doesn't work, the message title is "Re: need the skinny on my()
vs. local()", the message date is 12/04/1997. I found it by searching on
"tom, who can't believe anyone made it this far" which appears at the bottom
of the message, with my range set as the archives for comp.lang.perl.misc.

After referencing perlfaq7, Tom goes on to explain differences between local
& my. It's good stuff. As a matter of fact, at the time it was posted, I
read it, copied it, popped on the company intranet, and made sure everyone
read it and understood it.

------------------------------
Rick Brannan, Libronix Corp.
web: http://www.libronix.com
"I don't need no radio,
I got yodelin' on my noodle-o"
            -- Swirling Eddies

>-----Original Message-----
>From: Tim Maher/CONSULTIX [mailto:tim at consultix-inc.com]
>Sent: Thursday, February 17, 2000 9:14 PM
>To: spug-list at pm.org
>Subject: SPUG: Scoping Guidelines; comments?
>
>
>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


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