Patch for perlfaq4.pod

[email protected] Wed, 27 Aug 2008 15:16:22 +0200 (CEST)
Newsgroups perl.perlfaq.workers
Message-ID <[email protected]>
--5D221D7A068D11B060DDE1C04AF56D24
Content-Type: text/plain
Content-Transfer-Encoding: 8bit

David Nicol submitted a patch for perlfaq4.pod two years ago to the
Perl-RT (see http://rt.perl.org/rt3/Ticket/Display.html?id=40250).

I've attached the patch.

Do you apply this patch or is it rejected?

Cheers,
Renee

--5D221D7A068D11B060DDE1C04AF56D24
Content-Type: text/plain; name="faqpatch.diff"
Content-Transfer-Encoding: 8bit
Content-Disposition: attachment; filename="faqpatch.diff"

--- perl-5.9.4/pod/perlfaq4.pod	2006-08-15 07:37:41.000000000 -0500
+++ perl-5.9.4_dln/pod/perlfaq4.pod	2006-08-23 17:09:02.000000000 -0500
@@ -1988,22 +1988,37 @@
     my @keys = keys %myhash;
     # @keys = (0,1,2,3,...)
 
-=head2 Why does passing a subroutine an undefined element in a hash create it?
+=head2 Why does using an undefined hash element in a for/map/grep loop create it?
 
-If you say something like:
+That's because functions get scalars passed in by reference.  If somefunc() modifies C<$_[0]>,
+it has to be ready to write it back into the caller's version. The aliasing of $_
+to the value under consideration works similarly: in order to bind $_ to the 
+hash element, the hash element must be brought into existence.
+
+=head2 Why does passing a subroutine an undefined element in a hash not create it?
+
+A change was introduced, due to a brilliant patch,
+in Perl5.004, such that if you write 
+something like:
 
     somefunc($hash{"nonesuch key here"});
 
-Then that element "autovivifies"; that is, it springs into existence
-whether you store something there or not.  That's because functions
-get scalars passed in by reference.  If somefunc() modifies C<$_[0]>,
-it has to be ready to write it back into the caller's version.
-
-This has been fixed as of Perl5.004.
+Then that element does not autovivify, that is, spring into existence,
+until you store something there or otherwise do something with the
+parameter that would cause the undefined element to appear.
 
 Normally, merely accessing a key's value for a nonexistent key does
 I<not> cause that key to be forever there.  This is different than
-awk's behavior.
+awk's behavior.  
+
+If you really need a hash element to autovivify on getting passed to
+a function, you may use a hash slice: C<somefunc(@hash{qw/key/})>.  The
+hash slicing case was not affected by the 5.004 change.
+
+The point addressed in this and the previous question are subjects of heated
+debate: relying on a particular autovivification behavior when passing hash
+entries to subroutines or using them in lists passed to aliasing loop
+constructs is currently imprudent. In the future, these subtleties may be controlled by one or more pragmata.
 
 =head2 How can I make the Perl equivalent of a C structure/C++ class/hash or array of hashes or arrays?
 

--5D221D7A068D11B060DDE1C04AF56D24--