All opinions expressed are those of the authors and not necessarily those of OSNews.com, our sponsors, or our affiliates.
  Add to My Yahoo!  Subscribe with Bloglines  Subscribe in NewsGator Online

published by noreply@blogger.com (Ethan Rowe) on 2008-07-29 01:35:00 in the "hashref" category
We're preparing to upgrade from Perl 5.8.7 to 5.10.0 for a particular project, and ran into an interesting difference between the two versions. Consider the following statement for some hashref $attrib:
  use strict;
  ...
  my ($a, $b, $c) = @{%{$attrib}}{qw(a b c)};
In 5.8.7, the @{...} construct will return a slice of the hash referenced by $attrib, meaning that $a gets $attrib->{a}, $b gets $attrib->{b}, and so on. In 5.10.0, the same construct will result in an error complaining about using a string for a hashref. I suspect it's due to the hash dereference (%{$attrib}) being fully executed prior to applying the hash-slice operation (@{...}{qw(a b c)}), meaning that you're not operating against a hashref anymore. Fortunately, the fix is wonderfully simple and significantly more readable:
  my ($a, $b, $c) = @$attrib{qw( a b c )};
The "fix" -- which is arguably how it should have been constructed in the first place, but this is software we're talking about -- works in both versions of Perl.
Comments