Consider the following snippet:

#!/usr/bin/perl use Data::Dumper; my %h; { local $h{key} = 'blah'; # even "local $h{key};" will do # inititialization is only for clarity } print Dumper \%h;

In perl 5.6.1 the output is:

$VAR1 = { 'key' => undef };

while in 5.8.0 it is:

$VAR1 = {};

Digging through the documentation I found the following statement, in perldoc perlsub (in both 5.6.1 and 5.8.0 distributions):

The behavior of local() on non-existent members of composite types is subject to change in future.

Question 1: Can you provide me with some insight into the history of this development? Also, what lies ahead? The 5.8 behaviour looks more DWIMish IMHO - but the doc says that the behaviour may change. In which ways?

Question 2: While experimenting I wrote the following code to emulate 5.8 behaviour on 5.6:

# ... my %h; # ... my @local_keys = qw/foo bar baz/; my @inexistent = grep {! exists $h{$_}} @local_keys; { local @h{@local_keys}; # ... } delete @h{@inexistent}; # ...

You may notice the use of a hash slice as an argument to local. It came to me naturally (and it works). The question is: is this supported? My worries are based on my (possibly incorrect) belief that local and such are "special forms" and should be used only in ways sanctioned by the documentation (perldoc -f local is minimal and ambiguous). Also, why it works?


In reply to Perl 5.6 vs. 5.8: 'local' behaviour difference by calin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.