Yet another stupid mistake #3: local is not that magical

One of my favorite things in Perl is of course local variables, a.k.a. dynamic scoping. After learning that you can localize just a hash pair, or an array element, I have often used local() as sort of a stack to save temporary results. Then a few weeks ago I was hit with a bug:

{
    local $ary->[-1] = $foo;
    manipulate_ary($ary);
}

which is okay, until I got carried away and shift(), pop(), splice(), et al on @$ary in manipulate_ary(), and expected local() to take care of everything. Of course it's not that magical.

Leave a comment

About Steven Haryanto

user-pic A programmer (mostly Perl 5 nowadays). My CPAN ID: SHARYANTO. I'm sedusedan on perlmonks. My twitter is stevenharyanto (but I don't tweet much). Follow me on github: sharyanto.