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

Molybdenum Monks,

Uh, it's Twilight Zone time here. For three years this has been working fine:

foreach $group_id (keys %data) { @temp = sort keys %{ $data{$group_id} }; $my_hoa{$group_id}{'ordered'} = [ @temp ]; } foreach $group_id (sort keys %my_hoa) { for $i (0 .. $#{ @{ $my_hoa{$group_id}{'ordered'} } }) { # output some stuff; } }
Then a few days ago the loop stopped working. After adding
print "<p>array length = ".$#{ @{ $pubs{$lang_group_id}{'pub_alpha_ord +er'} } }."<p>\n";
at the beginning of the output loop, it became clear that

$#{ @{ $pubs{$lang_group_id}{'pub_alpha_order'} } }

was coming back as -1.

Weird, but ok, so just for argument's sake I changed it to

$#{ $pubs{$lang_group_id}{'pub_alpha_order'} }

And miracle of miracles, we're back in business. But, cue the Twilight Zone music, as I said, this has been working fine for years. Or has it?

Two possibilities - it never really worked, everyone just thinks they remember it working, or the server admins updated Perl and the new version refers to this kind of thing differently? So I'm inquiring about the second possiblity - did the way you refer to this change in a recent update of Perl? If not, we're checking the koolaid.

Thanks




Time flies like an arrow. Fruit flies like a banana.

Replies are listed 'Best First'.
Re: Something changed in how to refer to length of hash of arrays?
by ikegami (Patriarch) on May 06, 2009 at 17:20 UTC

    The expression in the curlies should evaluate to a reference (or variable name when strictures are off). You were relying on a case where this was broken. Looks like the bug was fixed in 5.10.0:

    >perl589\bin\perl -le"@8 = 0..5; @a = 0..7; print $#{ @a }" 7 >perl5100\bin\perl -le"@8 = 0..5; @a = 0..7; print $#{ @a }" 5

    See Dereferencing Syntax and References Quick Reference

    was coming back as -1.

    Only because you disabled strictures. (boooo!)

    >perl -wle"no strict; my @a = qw( a b c ); print $#{ @a }" -1 >perl -wle"use strict; my @a = qw( a b c ); print $#{ @a }" Can't use string ("3") as an ARRAY ref while "strict refs" in use at - +e line 1.

    Update: Added examples.

Re: Something changed in how to refer to length of hash of arrays?
by JavaFan (Canon) on May 06, 2009 at 18:02 UTC
    or the server admins updated Perl and the new version refers to this kind of thing differently? So I'm inquiring about the second possiblity - did the way you refer to this change in a recent update of Perl?
    Wouldn't it be more efficient if you first check whether your perl was upgraded before asking such questions?
Re: Something changed in how to refer to length of hash of arrays?
by GrandFather (Saint) on May 07, 2009 at 03:16 UTC