Use local instead of my in Perl when using $$ (double dollar sign) inside an enclosed block

I thought I would be all clever and initialize several hash indices at once using the $$ notation in Perl - evaluating strings as variables. Unfortunately using "my" screws this up bigtime.

This must be another circumstance spelled out in the incomprehensible "my" treatise:
http://perldoc.perl.org/perlsub.html#Private-Variables-via-my()


my %foo;
$foo{'bar'}=1;
foreach $fooDex(qw(foo)){
$$fooDex{'bar'}=2;
}
print $foo{'bar'}."\n"; #prints 1




local %foo;
$foo{'bar'}=1;
foreach $fooDex(qw(foo)){
$$fooDex{'bar'}=2;
}
print $foo{'bar'}."\n"; #prints 2


This is one of those solutions that gives you a feeling of dread instead of accomplishment.

Comments

  1. Betfair Sportsbook Review 2021 - SmFS.Info
    Betfair Sportsbook is one of the biggest names in the online betting industry, but they have managed 온라인카지노 to attract the very rich players from Casino type: Football, Racing Rating: 4 · 바카라사이트 ‎Review by SmFS

    ReplyDelete

Post a Comment

Popular Posts