Skip to content

Instantly share code, notes, and snippets.

@barefootcoder
Created September 15, 2013 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barefootcoder/6574445 to your computer and use it in GitHub Desktop.
Save barefootcoder/6574445 to your computer and use it in GitHub Desktop.
A bit of code to demonstrate how the ambiguity between auto-deferencing hashrefs and arrayrefs can be caught.
#! /usr/bin/env perl
# a standard prefix for modern Perl
use 5.014; # implies both `use strict` and `use feature :5.14`
use autodie qw< :all >;
use warnings FATAL => 'all';
my $arrayref = [ qw< foo bar baz bmoogle > ];
my $hashref = { map { $_ => 1 } @$arrayref };
say 'hash: ', join(', ', keys $hashref); # works fine
say 'array: ', join(', ', keys $arrayref); # also works fine
my $corresponding_arrayref = $hashref; # oops!
foreach (keys $corresponding_arrayref)
{
say $arrayref->[$_]; # kaboom!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment