Skip to content

Instantly share code, notes, and snippets.

@dstu
Forked from anonymous/gist:4489522
Last active December 10, 2015 20:38
Show Gist options
  • Save dstu/4489529 to your computer and use it in GitHub Desktop.
Save dstu/4489529 to your computer and use it in GitHub Desktop.
Is this a bug in Perl?
use strict;
use warnings;
use Data::Dumper;
my $things = { foo => 'bar' };
foreach my $word (qw(foo baz bar quux)) {
my $count = grep { defined } $things->{$word};
print "$count\n";
}
# $things now has entries for foo, bar, baz and quux
# tested on:
# perl 5, version 14, subversion 1 (v5.14.1) built for x86_64-linux
# perl 5, version 14, subversion 2 (v5.14.2) built for cygwin-thread-multi-64int (with 7 registered patches)
# perl, v5.8.8 built for x86_64-linux-thread-multi
print Dumper($things);
__END__
@dstu
Copy link
Author

dstu commented Jan 9, 2013

No, it's not a bug. It's Perl's autovivification doing funny things.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment