[pm-h] Perl Gotchas

Michael R. Davis mrdvt92 at yahoo.com
Mon Mar 16 21:23:32 PDT 2015


Julian, > So if you have something that bit you in the past I would like to hear about it.   $ perl -Mstrict -Mwarnings -e 'my %hash=(A=>1, B=>2); print $hash{shift}, "\n"' B
Use of uninitialized value $hash{"shift"} in print at -e line 1. Uninitialized? B is 2! Perl, what are you thinking?
This bit me hard as it took a long time to figure out (actually a colleague did ).  The "shift" is actually 'shift' not shift(). I wish there we an ultra strict mode which does not allow bare words for hash keys. I now only use two syntaxes for hash keys. Once bitten twice shy. $hash{'A'};$hash{$key}; Example, of what I would do today.  First, always shift/set variables at the start of every script/sub with sane defaults. $ perl -Mstrict -Mwarnings -e 'my $key=shift || "A"; my %hash=(A=>1, B=>2); print $hash{$key}, "\n"' B
2
Thanks,Mike  mrdvt92 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/houston/attachments/20150317/5c52637a/attachment.html>


More information about the Houston mailing list