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

Mathew Robertson mathew.blair.robertson at gmail.com
Mon Jul 25 23:07:55 PDT 2011


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?

regards,
Mathew Robertson


#!/usr/bin/perl
use strict;
use warnings FATAL => 'all';
use Data::Dumper; $Data::Dumper::Indent = 1; $Data::Dumper::Sortkeys = 1;
our %HANDLERS;

# This block, as expected, de-localises %HANDLERS at block scope.

$HANDLERS{a} = sub {};
$HANDLERS{b} = sub {};
print Dumper(\%HANDLERS);
{
  local %HANDLERS = %HANDLERS;
  $HANDLERS{c} = 't';
  print Dumper(\%HANDLERS);
}
print Dumper(\%HANDLERS);

print "-" x 50 . $/;

# Unexpectedly, no de-localising of %HANDLERS at block scope.
# I would have expected $HANDLERS{c} to be localised.

$HANDLERS{c} = ['t'];

print Dumper(\%HANDLERS);
{
#  local %HANDLERS = %HANDLERS;
  local $HANDLERS{c} = $HANDLERS{c};
#  local @{$HANDLERS{c}} = @{$HANDLERS{c}};
  push @{$HANDLERS{c}}, 'u';
  print Dumper(\%HANDLERS);
}
print Dumper(\%HANDLERS);
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/melbourne-pm/attachments/20110726/511ce4dd/attachment.html>


More information about the Melbourne-pm mailing list