calin has asked for the wisdom of the Perl Monks concerning the following question:

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?

Replies are listed 'Best First'.
Re: Perl 5.6 vs. 5.8: 'local' behaviour difference
by RMGir (Prior) on May 20, 2004 at 17:19 UTC
    It looks like the 5.6 behaviour was declared to be a bug in December 2000, and patched by Simon Cozens by January 2001.

    See this p5p message and the thread it's in for more details.

    Judging from that thread, I think you can count on the 5.8 behaviour being the "correct" one, although I think there was some later debate about what "DTRT" means for tied or magical hashes.


    Mike, who's SO glad he downloads the p5p mbox files so he can grepmail thru them...
Re: Perl 5.6 vs. 5.8: 'local' behaviour difference
by hv (Prior) on May 20, 2004 at 23:00 UTC

    The keyword here is "autovivification", which is the jargon word used within the perl community to describe situations in which something referred to without explicitly being created is caused to spring into existence. Knowing that word may help you to search for and understand more of the history of this particular change and how it might change in the future.

    One possible way that behaviour might change in the future is to extend the 5.8.0 fix to deeper structures:

    my %h; { local $h{a}{b} = 1; } use Data::Dumper; print Dumper(\%h);
    which shows that while $h{a}{b} has gone away, $h{a} still exists as an empty hashref.

    For the second question, I can assure you that your use of hash slices in local and delete is perfectly legal and will not be going away. The key phrase of the brief documentation in perlfunc is A local modifies the listed variables ... - the hash slice is providing such a list of variables, and each of those variables is individually being localised.

    It is however confusing that it goes on to say that the list must be parenthesised if more than one variable is listed - a single hash or array slice is an exception to that rule.

    Hugo