in reply to hash dereferencing issue with perl 5.16.3

Further to toolic's post, the change seems to have occurred between 5.8.9 and 5.10.1.

c:\@Work\Perl>perl -wMstrict -le "print qq{perl version $]}; ;; my %h = qw(a aye b bee); my $hr = \%h; ;; print ${ %{$hr} }{a}; print ${ %h }{b}; " perl version 5.008009 aye bee c:\@Work\Perl>perl -wMstrict -le "print qq{perl version $]}; ;; my %h = qw(a aye b bee); my $hr = \%h; ;; print ${ %{$hr} }{a}; print ${ %h }{b}; " perl version 5.010001 Can't use string ("2/8") as a HASH ref while "strict refs" in use at - +e line 1.
Checking perl5100delta and perl5101delta reveals no explanation for the change.

Update: Note that similar syntax works/doesn't work for arrays:

c:\@Work\Perl>perl -wMstrict -le "print qq{perl version $]}; ;; my @ra = qw(zero one two); my $ar = \@ra; ;; print ${ @ra }[1]; print ${ @$ar }[2]; " perl version 5.008009 one two c:\@Work\Perl>perl -wMstrict -le "print qq{perl version $]}; ;; my @ra = qw(zero one two); my $ar = \@ra; ;; print ${ @ra }[1]; print ${ @$ar }[2]; " perl version 5.010001 Can't use string ("3") as an ARRAY ref while "strict refs" in use at - +e line 1.


Give a man a fish:  <%-(-(-(-<

Replies are listed 'Best First'.
Re^2: hash dereferencing issue with perl 5.16.3
by KANAKADANDI (Initiate) on May 03, 2015 at 01:05 UTC

    thanks a lot for the answers. So i would take it as there is no settings that would enable me to work with my existing Perl scripts without changing them. I would appreciate if someone could suggest me if there is an easy to identify the programs which have these symbolic references. I appreciate your help.

      As far as identifying them goes, you could grep your files for ${%{$ . That sequence of characters isn't likely to show up anywhere else except in these constructs.

      Automating a change would be more tricky, but could probably be done. Something like this which you could filter all problem files through:

      #!/usr/bin/env perl use strict; use warnings; while(<DATA>){ print "before: $_";; s|\${%{\$(\w+)}}{'(\w+)'}|\$${1}->{'$2'}|g; print " after: $_";; } __DATA__ my $lValue = ${%{$lHashRef}}{'a'};

      That might not work on all your variable names or if some are formatted a bit differently, but it could be a start.

      Aaron B.
      Available for small or large Perl jobs and *nix system administration; see my home node.

        That might not work on all your variable names or if some are formatted a bit differently ...

        In particular, note that it's possible to go a little nuts with whitespace and the Perl compiler still calmly accepts it. That, and the same basic extinct syntax also works with arrays and array references, which KANAKADANDI has never said are not present in the code.

        c:\@Work\Perl>perl -wMstrict -le "print qq{perl version $]}; ;; my @ra = qw(a b c d); my $ar = \@ra; print ${ ra }[2]; print ${ @ ra }[2]; print ${ $ ar }[2]; print ${ @ $ar }[2]; print ${ @ { $ ar } }[2]; ;; my %h = qw(a aye b bee c see); my $hr = \%h; print ${ h }{b}; print ${ % h }{b}; print ${ $ hr }{b}; print ${ % $hr }{b}; print ${ % { $ hr } }{b}; " perl version 5.008009 c c c c c bee bee bee bee bee

        Another point I would make is that a search for these extinct forms should be based on a rigorous definition of a Perl identifier (insofar as one can be defined in user code):
            my $identifier = qr{ \b [[:alpha:]_] \w* \b }xms;
        (or that's my first cut, anyway — but it doesn't cover "fully qualified" identifiers). See Variable names in perldata.

        I think the best automated search-and-replace is still going to need a final manual patching pass running with strictures enabled, which I sure hope KANAKADANDI is able to do!


        Give a man a fish:  <%-(-(-(-<

      thanks a lot for the answers. So i would take it as there is no settings that would enable me to work with my existing Perl scripts without changing them. I would appreciate if someone could suggest me if there is an easy to identify the programs which have these symbolic references. I appreciate your help.

      See references quick reference and use the syntax that is documented to be valid, all other syntax isn't even if it happened to work on somehow at sometime

      suggest me if there is an easy to identify the programs which have these symbolic references.
      Very easy: just run your tests. You do have test suites with high coverage, right? :)