t/op/grep.t using test.pl
[p5sagit/p5-mst-13.2.git] / pod / perlfaq7.pod
index 644b065..9d9ef8d 100644 (file)
@@ -1,6 +1,6 @@
 =head1 NAME
 
-perlfaq7 - General Perl Language Issues ($Revision: 1.23 $, $Date: 2005/04/07 21:39:34 $)
+perlfaq7 - General Perl Language Issues ($Revision: 3606 $)
 
 =head1 DESCRIPTION
 
@@ -179,7 +179,7 @@ If you're looking for something a bit more rigorous, try L<perltoot>.
 (contributed by brian d foy)
 
 L<perlmod>, L<perlmodlib>, L<perlmodstyle> explain modules
-in all the gory details. L<perlnewmod> gives a a brief
+in all the gory details. L<perlnewmod> gives a brief
 overview of the process along with a couple of suggestions
 about style.
 
@@ -271,24 +271,40 @@ $line back in its caller's scope.
 
 =head2 What is variable suicide and how can I prevent it?
 
-Variable suicide is when you (temporarily or permanently) lose the
-value of a variable.  It is caused by scoping through my() and local()
-interacting with either closures or aliased foreach() iterator
-variables and subroutine arguments.  It used to be easy to
-inadvertently lose a variable's value this way, but now it's much
-harder.  Take this code:
+This problem was fixed in perl 5.004_05, so preventing it means upgrading
+your version of perl. ;)
 
-    my $f = "foo";
+Variable suicide is when you (temporarily or permanently) lose the value
+of a variable.  It is caused by scoping through my() and local()
+interacting with either closures or aliased foreach() iterator variables
+and subroutine arguments.  It used to be easy to inadvertently lose a
+variable's value this way, but now it's much harder.  Take this code:
+
+    my $f = 'foo';
     sub T {
-      while ($i++ < 3) { my $f = $f; $f .= "bar"; print $f, "\n" }
+      while ($i++ < 3) { my $f = $f; $f .= $i; print $f, "\n" }
     }
     T;
     print "Finally $f\n";
 
+If you are experiencing variable suicide, that C<my $f> in the subroutine
+doesn't pick up a fresh copy of the C<$f> whose value is <foo>. The output
+shows that inside the subroutine the value of C<$f> leaks through when it
+shouldn't, as in this output:
+
+       foobar
+       foobarbar
+       foobarbarbar
+       Finally foo
+
 The $f that has "bar" added to it three times should be a new C<$f>
-(C<my $f> should create a new local variable each time through the loop).
-It isn't, however.  This was a bug, now fixed in the latest releases
-(tested against 5.004_05, 5.005_03, and 5.005_56).
+C<my $f> should create a new lexical variable each time through the loop.
+The expected output is:
+
+       foobar
+       foobar
+       foobar
+       Finally foo
 
 =head2 How can I pass/return a {Function, FileHandle, Array, Hash, Method, Regex}?
 
@@ -418,7 +434,7 @@ the function in which they are declared. You can get the same effect
 with lexical variables, though.
 
 You can fake a static variable by using a lexical variable which goes
-of scope. In this example, you define the subroutine C<counter>, and
+out of scope. In this example, you define the subroutine C<counter>, and
 it uses the lexical variable C<$count>. Since you wrap this in a BEGIN
 block, C<$count> is defined at compile-time, but also goes out of
 scope at the end of the BEGIN block. The BEGIN block also ensures that
@@ -944,9 +960,17 @@ If you get a message like "perl: command not found", perl is not in
 your PATH, which might also mean that the location of perl is not
 where you expect it so you need to adjust your shebang line.
 
+=head1 REVISION
+
+Revision: $Revision: 3606 $
+
+Date: $Date: 2006-03-06 12:05:47 +0100 (lun, 06 mar 2006) $
+
+See L<perlfaq> for source control details and availability.
+
 =head1 AUTHOR AND COPYRIGHT
 
-Copyright (c) 1997-2005 Tom Christiansen, Nathan Torkington, and
+Copyright (c) 1997-2006 Tom Christiansen, Nathan Torkington, and
 other authors as noted. All rights reserved.
 
 This documentation is free; you can redistribute it and/or modify it