mstet has asked for the wisdom of the Perl Monks concerning the following question:

The code below works on perl 5.8.1, but gives the error "can't use string ("13") as an ARRAY ref while 'strict refs' in use" when executuing the sort command at the bottom with version 5.10. Help would be appreciated

my $filename = shift; my $refdataarr = shift; my $rows; my $cols; my $Excel; my $Book; my $Sheet; my @files; my @title = (qw/File QAC_off QAC_nod Lvl_9 Lvl_8 Lvl_7 Lvl_6 Lvl_5 L +vl_4 Lvl_3 Lvl_2 Lvl_1 Lvl_0 Sum/ ); my $total_cols = @title; # 14 if ( -f $filename ) { if (! unlink($filename) ) { printf("Can not delete '%s'. Excel file not created!\n", $fi +lename); return; } } $Excel = Win32::OLE->new("Excel.Application",sub {$_[0]->Quit;}) or die "Oops, cannot start Excel"; $Book = $Excel->Workbooks->Add; $Sheet = $Book->Worksheets(1); @files = sort { lc(${@$a}[0]) cmp lc(${@$b}[0]) } @$refdataarr ;

Replies are listed 'Best First'.
Re: "strict" violation on "sort" command with perl 5.10
by jettero (Monsignor) on Apr 28, 2009 at 19:42 UTC
    At least one of the elements of @$refdataarr isn't an array ref (it's a 13), so @$a is failing. It couldn't be clearer, and you really only needed the one line of code -- since we can't see what's populating $refdataarr anyway.
    eval { use strict; () = sort { @$a <=> @$b } ([1], [2], 13) }; warn "Your error I believe: $@";

    -Paul

      Or $refdataarr holds 13 instead of an array ref.

      Either way, it usually happens when one does
      $ref = @array;
      when one needs to do
      $ref = \@array;
      or
      $ref = [ @array ];

      Contrary to what the OP said, it has nothing to do with 5.10. Something else changed too.

        From the OP:
        ${@$a}[0]
        I had to stare at that for a bit and then do a little experiment:
        >perl -wMstrict -le "my @ra = qw(a b c d); my $ar = \@ra; print ${ ra}[2]; print ${ @ra}[2]; print ${ $ar}[2]; print ${@$ar}[2]; " c c c c
        Sure enough, it works! I'm gobsmacked!

        Needless to say, I've never seen the second and fourth forms of the array access expression, the ones using the  @ sigil apparently redundantly. These forms are certainly not widely used, but are they ever used? I.e., is there any situation in which the  @ sigil must be used in this way?

        Update: Added $ that was MIA in ${@$a}[0].

Re: "strict" violation on "sort" command with perl 5.10
by jwkrahn (Abbot) on Apr 28, 2009 at 20:01 UTC

    Change:

    @files = sort { lc(${@$a}[0]) cmp lc(${@$b}[0]) } @$refdataarr ;

    To:

    @files = sort { lc $a->[0] cmp lc $b->[0] } @$refdataarr ;

    You are dereferencing the array reference in $a and $b as @$a and @$b and then trying to dereference that array as an array reference.