SPUG: Scoping Guidelines; comments?

Tim Maher/CONSULTIX tim at consultix-inc.com
Fri Feb 18 11:04:41 CST 2000


On Fri, Feb 18, 2000 at 08:04:15AM -0800, Phillip Neal wrote:
> Tim,

Just to be clear on this, are you saying one should 
	*Suspect Everything is Global*?

-Tim
($::Tim, $main::Tim, etc.)

> 
> My rules of thumb have always been:
> 
> 1. Suspect everything is global
> 
> 2. Use clever names in main for all variables. After all, it's
>    not Fortran IV anymore.
> 
> 3. Use even more clever names for variables in subroutines.
> 
> 4. Avoid using $_ for all but the most trivial cases,especially
>    in subroutines.
> 
> 5. Use lot's of print statements when debugging so you can
>    see exactly what you're playing with.
> 
> 6. Suspect everything is global.
> 
> 7. Suspect everything is global.
> 
> And most of all, suspect everything is global.
> 
> Phil
> 
> 
> >
> >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
> >}
> >
> 
> ______________________________________________________
> Get Your Private, Free Email at http://www.hotmail.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
> 
> 

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





More information about the spug-list mailing list