Patch to make code use strict where appropriate and be generally
pleasing to the eye, and easy to understand.

  Casey West

-- 
Shooting yourself in the foot with Lisp 
You shoot yourself in the appendage which holds the gun with which you
shoot yourself in the appendage which holds the gun with which you
shoot yourself in the appendage which holds... 
diff -u perl-current.orig/pod/perldata.pod perl-current/pod/perldata.pod
--- perl-current.orig/pod/perldata.pod  Thu Sep 13 21:42:52 2001
+++ perl-current/pod/perldata.pod       Fri Sep 14 14:26:12 2001
@@ -224,7 +224,7 @@
 can truncate an array down to nothing by assigning the null list
 () to it.  The following are equivalent:
 
-    @whatever = ();
+    my @whatever = ();
     $#whatever = -1;
 
 If you evaluate an array in scalar context, it returns the length
@@ -233,13 +233,6 @@
 which return whatever they feel like returning.)  The following is
 always true:
 
-    scalar(@whatever) == $#whatever - $[ + 1;
-
-Version 5 of Perl changed the semantics of C<$[>: files that don't set
-the value of C<$[> no longer need to worry about whether another
-file changed its value.  (In other words, use of C<$[> is deprecated.)
-So in general you can assume that
-
     scalar(@whatever) == $#whatever + 1;
 
 Some programmers choose to use an explicit conversion so as to 
@@ -261,6 +254,7 @@
 You can preallocate space for a hash by assigning to the keys() function.
 This rounds up the allocated buckets to the next power of two:
 
+    my %users = ();
     keys(%users) = 1000;               # allocate 1024 buckets
 
 =head2 Scalar value constructors
@@ -306,8 +300,8 @@
 expression as a subscript.)  The following code segment prints out "The
 price is $Z<>100."
 
-    $Price = '$100';   # not interpreted
-    print "The price is $Price.\n";    # interpreted
+    my $Price = '$100';                 # not interpolated
+    print "The price is $Price.\n";    # interpolated
 
 As in some shells, you can enclose the variable name in braces to
 disambiguate it from following alphanumerics (and underscores).
@@ -316,7 +310,7 @@
 variable name from a following double-colon or an apostrophe, since
 these would be otherwise treated as a package separator:
 
-    $who = "Larry";
+    my $who = "Larry";
     print PASSWD "${who}::0:0:Superuser:/:/bin/perl\n";
     print "We use ${who}speak when ${who}'s here.\n";
 
@@ -399,7 +393,7 @@
 variable (C<$LIST_SEPARATOR> in English), space by default.  The
 following are equivalent:
 
-    $temp = join($", @ARGV);
+    my $temp = join($", @ARGV);
     system "echo $temp";
 
     system "echo @ARGV";
@@ -466,7 +460,7 @@
 rest of the code, you'll need to remove leading whitespace
 from each line manually:
 
-    ($quote = <<'FINIS') =~ s/^\s+//gm;
+    (my $quote = <<'FINIS') =~ s/^\s+//gm;
        The Road goes ever on and on, 
        down from the door where it began.
     FINIS
@@ -515,23 +509,23 @@
 to be a list literal is simply the value of the final element, as
 with the C comma operator.  For example,
 
-    @foo = ('cc', '-E', $bar);
+    my @foo = ('cc', '-E', $bar);
 
 assigns the entire list value to array @foo, but
 
-    $foo = ('cc', '-E', $bar);
+    my $foo = ('cc', '-E', $bar);
 
 assigns the value of variable $bar to the scalar variable $foo.
 Note that the value of an actual array in scalar context is the
 length of the array; the following assigns the value 3 to $foo:
 
-    @foo = ('cc', '-E', $bar);
-    $foo = @foo;               # $foo gets 3
+    my @foo = ('cc', '-E', $bar);
+    my $foo = @foo;            # $foo gets 3
 
 You may have an optional comma before the closing parenthesis of a
 list literal, so that you can say:
 
-    @foo = (
+    my @foo = (
        1,
        2,
        3,
@@ -540,7 +534,7 @@
 To use a here-document to assign an array, one line per element,
 you might use an approach like this:
 
-    @sauces = <<End_Lines =~ m/(\S.*\S)/g;
+    my @sauces = <<End_Lines =~ m/(\S.*\S)/g;
        normal tomato
        spicy tomato
        green chile
@@ -579,13 +573,13 @@
 put the list in parentheses to avoid ambiguity.  For example:
 
     # Stat returns list value.
-    $time = (stat($file))[8];
+    my $time = (stat($file))[8];
 
     # SYNTAX ERROR HERE.
-    $time = stat($file)[8];  # OOPS, FORGOT PARENTHESES
+    my $time = stat($file)[8];  # OOPS, FORGOT PARENTHESES
 
     # Find a hex digit.
-    $hexdigit = ('a','b','c','d','e','f')[$digit-10];
+    my $hexdigit = ('a','b','c','d','e','f')[$digit-10];
 
     # A "reverse comma operator".
     return (pop(@foo),pop(@foo))[0];
@@ -593,21 +587,21 @@
 Lists may be assigned to only when each element of the list
 is itself legal to assign to:
 
-    ($a, $b, $c) = (1, 2, 3);
+    my($a, $b, $c) = (1, 2, 3);
 
-    ($map{'red'}, $map{'blue'}, $map{'green'}) = (0x00f, 0x0f0, 0xf00);
+    ($map{red}, $map{blue}, $map{green}) = (0x00f, 0x0f0, 0xf00);
 
 An exception to this is that you may assign to C<undef> in a list.
 This is useful for throwing away some of the return values of a
 function:
 
-    ($dev, $ino, undef, undef, $uid, $gid) = stat($file);
+    my($dev, $ino, undef, undef, $uid, $gid) = stat($file);
 
 List assignment in scalar context returns the number of elements
 produced by the expression on the right side of the assignment:
 
-    $x = (($foo,$bar) = (3,2,1));      # set $x to 3, not 2
-    $x = (($foo,$bar) = f());          # set $x to f()'s return count
+    my $x = (($foo,$bar) = (3,2,1));   # set $x to 3, not 2
+    my $x = (($foo,$bar) = f());        # set $x to f()'s return count
 
 This is handy when you want to do a list assignment in a Boolean
 context, because most list functions return a null list when finished,
@@ -618,7 +612,7 @@
 return values, by assigning to an empty list and then using that
 assignment in scalar context. For example, this code:
 
-    $count = () = $string =~ /\d+/g;
+    my $count = () = $string =~ /\d+/g;
 
 will place into $count the number of digit groups found in $string.
 This happens because the pattern match is in list context (since it
@@ -628,14 +622,15 @@
 number of times the pattern matched) and assign that to $count. Note
 that simply using
 
-    $count = $string =~ /\d+/g;
+    my $count = $string =~ /\d+/g;
 
 would not have worked, since a pattern match in scalar context will
 only return true or false, rather than a count of matches.
 
 The final element of a list assignment may be an array or a hash:
 
-    ($a, $b, @rest) = split;
+    my($a, $b, @rest) = split;
+    # or
     my($a, $b, %rest) = @_;
 
 You can actually put an array or hash anywhere in the list, but the first one
@@ -646,7 +641,7 @@
 items to be interpreted as a key and a value:
 
     # same as map assignment above
-    %map = ('red',0x00f,'blue',0x0f0,'green',0xf00);
+    my %map = ('red',0x00f,'blue',0x0f0,'green',0xf00);
 
 While literal lists and named arrays are often interchangeable, that's
 not the case for hashes.  Just because you can subscript a list value like
@@ -661,7 +656,7 @@
 interpreted as a string--if it's a bareword that would be a legal identifier.
 This makes it nice for initializing hashes:
 
-    %map = (
+    my %map = (
                 red   => 0x00f,
                 blue  => 0x0f0,
                 green => 0xf00,
@@ -669,7 +664,7 @@
 
 or for initializing hash references to be used as records:
 
-    $rec = {
+    my $rec = {
                witch => 'Mable the Merciless',
                cat   => 'Fluffy the Ferocious',
                date  => '10/31/1776',
@@ -677,12 +672,14 @@
 
 or for using call-by-named-parameter to complicated functions:
 
-   $field = $query->radio_group(
+   use CGI;
+   my $query = CGI->new;
+   my $field = $query->radio_group(
               name      => 'group_name',
                values    => ['eenie','meenie','minie'],
                default   => 'meenie',
                linebreak => 'true',
-               labels    => \%labels
+               labels    => \%labels,
    );
 
 Note that just because a hash is initialized in that order doesn't
@@ -694,34 +691,36 @@
 A common way to access an array or a hash is one scalar element at a
 time.  You can also subscript a list to get a single element from it.
 
-    $whoami = $ENV{"USER"};            # one element from the hash
-    $parent = $ISA[0];                 # one element from the array
-    $dir    = (getpwnam("daemon"))[7]; # likewise, but with list
+    my $whoami = $ENV{"USER"};           # one element from the hash
+    my $parent = $ISA[0];                # one element from the array
+    my $dir    = (getpwnam("daemon"))[7]; # likewise, but with list
 
 A slice accesses several elements of a list, an array, or a hash
 simultaneously using a list of subscripts.  It's more convenient
 than writing out the individual elements as a list of separate
 scalar values.
 
-    ($him, $her)   = @folks[0,-1];             # array slice
-    @them          = @folks[0 .. 3];           # array slice
-    ($who, $home)  = @ENV{"USER", "HOME"};     # hash slice
-    ($uid, $dir)   = (getpwnam("daemon"))[2,7];        # list slice
+    my($him, $her)   = @folks[0,-1];             # array slice
+    my @them         = @folks[0 .. 3];           # array slice
+    my($who, $home)  = @ENV{"USER", "HOME"};     # hash slice
+    my($uid, $dir)   = (getpwnam("daemon"))[2,7]; # list slice
 
 Since you can assign to a list of variables, you can also assign to
 an array or hash slice.
 
-    @days[3..5]    = qw/Wed Thu Fri/;
+    my( @days, %colors, @folks );
+    @days[3..5]    = qw(Wed Thu Fri);
     @colors{'red','blue','green'} 
                   = (0xff0000, 0x0000ff, 0x00ff00);
     @folks[0, -1]  = @folks[-1, 0];
 
 The previous assignments are exactly equivalent to
 
-    ($days[3], $days[4], $days[5]) = qw/Wed Thu Fri/;
-    ($colors{'red'}, $colors{'blue'}, $colors{'green'})
+    my( @days, %colors, @folks );
+    ($days[3], $days[4], $days[5]) = qw(Wed Thu Fri);
+    ($colors{red}, $colors{blue}, $colors{green})
                   = (0xff0000, 0x0000ff, 0x00ff00);
-    ($folks[0], $folks[-1]) = ($folks[0], $folks[-1]);
+    ($folks[0], $folks[-1]) = ($folks[-1], $folks[0]);
 
 Since changing a slice changes the original array or hash that it's
 slicing, a C<foreach> construct will alter some--or even all--of the
@@ -737,19 +736,19 @@
 
 A slice of an empty list is still an empty list.  Thus:
 
-    @a = ()[1,0];           # @a has no elements
-    @b = (@a)[0,1];         # @b has no elements
-    @c = (0,1)[2,3];        # @c has no elements
+    my @a = ()[1,0];           # @a has no elements
+    my @b = (@a)[0,1];         # @b has no elements
+    my @c = (0,1)[2,3];        # @c has no elements
 
 But:
 
-    @a = (1)[1,0];          # @a has two elements
-    @b = (1,undef)[1,0,2];  # @b has three elements
+    my @a = (1)[1,0];          # @a has two elements
+    my @b = (1,undef)[1,0,2];  # @b has three elements
 
 This makes it easy to write loops that terminate when a null list
 is returned:
 
-    while ( ($home, $user) = (getpwent)[7,0]) {
+    while ( my($home, $user) = (getpwent)[7,0] ) {
        printf "%-8s %s\n", $user, $home;
     }
 
@@ -776,6 +775,8 @@
 The main use of typeglobs in modern Perl is create symbol table aliases.
 This assignment:
 
+    {
+     
     *this = *that;
 
 makes $this an alias for $that, @this an alias for @that, %this an alias
@@ -788,17 +789,18 @@
 make @Here::blue an alias for @There::green, or %Here::blue an alias for
 %There::green, etc.  See L<perlmod/"Symbol Tables"> for more examples
 of this.  Strange though this may seem, this is the basis for the whole
-module import/export system.
+module import/export system.  And none of it works under
+C<use strict 'vars'>.
 
 Another use for typeglobs is to pass filehandles into a function or
 to create new filehandles.  If you need to use a typeglob to save away
 a filehandle, do it this way:
 
-    $fh = *STDOUT;
+    my $fh = *STDOUT;
 
 or perhaps as a real reference, like this:
 
-    $fh = \*STDOUT;
+    my $fh = \*STDOUT;
 
 See L<perlsub> for examples of using these as indirect filehandles
 in functions.
@@ -813,7 +815,7 @@
        open   (FH, $path)          or  return undef;
        return *FH;
     }
-    $fh = newopen('/etc/passwd');
+    my $fh = newopen('/etc/passwd');
 
 Now that we have the C<*foo{THING}> notation, typeglobs aren't used as much
 for filehandle manipulations, although they're still needed to pass brand
@@ -833,8 +835,9 @@
 that must be passed around, as in the following example:
 
     sub myopen {
-        open my $fh, "@_"
-            or die "Can't open '@_': $!";
+        my $filename = shift;
+        open my $fh, $filename
+            or die "Can't open '$filename': $!";
        return $fh;
     }
 

Reply via email to