public
Created

A bit of code to demonstrate how the ambiguity between auto-deferencing hashrefs and arrayrefs can be caught.

  • Download Gist
hashref-arrayref.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#! /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!
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.