[Melbourne-pm] quirky behaviour of 'local'...

Mathew Robertson mathew.blair.robertson at gmail.com
Tue Jul 26 00:00:54 PDT 2011

On 26 July 2011 16:27, Toby Corkindale <toby.corkindale at strategicdata.com.au
> wrote:

> On 26/07/11 16:07, Mathew Robertson wrote:
>> Hi all,
>> As a result of some hacking on some code, I identified some quirky
>> behaviour with regards to 'local'...   I have whittled the problem down
>> to it simplest form - see below.
>> Basically speaking, I would have expected the inner-array-variable to
>> become localised to block scope.  Is there something that I am missing
>> here?
> What is happening here is a quirk of how Perl does multi-level data
> structures..
> $HANDLERS{c} contains a reference to an array.
> When you localise $HANDLERS{c} you are localising the value of that
> reference.. not the actual data in the referenced thing.

Indeed that looks to be the case... which is why in the posted example, I
also attempted to the use the localised dereferenced array (commented out),
but to no avail as I am probably localising the array ref.

> You can achieve the result you're looking for with this mess:
> use Storable qw(dclone);
> {
>  # local $HANDLERS{c} = $HANDLERS{c};
>  local $HANDLERS{c} = dclone($HANDLERS{c});
>  push @{$HANDLERS{c}}, 'u';
>  print Dumper(\%HANDLERS);
> }

Not directly related the original question, but somewhat relevant I
guess.... How does Storable::dclone compare to Clone::clone ?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/melbourne-pm/attachments/20110726/916072a6/attachment.html>

More information about the Melbourne-pm mailing list