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

I wonder is the following construction legal?
my %a; @a{ my @b } = ();
It works fine with:
perl -wle 'use Data::Dumper; my %a; @a{my @b=()} = (); print Dumper(\% +a)'
 Output:
$VAR1 = {};
But it's NOT work with perl, v5.8.4 built for i686-linux-thread-multi
 Output:
Use of uninitialized value in hash slice at -e line 1.
$VAR1 = {
'' => undef
};

Replies are listed 'Best First'.
Re: using "my" in hash slice
by dave_the_m (Monsignor) on Jun 18, 2004 at 09:43 UTC
    The spurious warning is caused by a a bug in 5.8.4 that is fixed in the forthcoming 5.8.5.

    Dave.

Re: using "my" in hash slice
by PodMaster (Abbot) on Jun 18, 2004 at 09:28 UTC
    Yes it is legal (pretty dumb, but legal).
    But it's NOT work with perl, v5.8.4 built for i686-linux-thread-multi
    Of course it works. Sure it does something different, but it works. The real question is what is it supposed to do? If you had written @a{(undef) } = () you'd have gotten the same result. I would guess that this "new" behaviour is the correct behaviour (all expressions which evaluate to undef should be turned into the empty string "").

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      If you had written @a{(undef) } = () you'd have gotten the same result.
      But (undef) and () are the different things. The first one is not empty list, but the second one is empty.
      BTW the following code gives the identical results with all versions of the perl (no warninigs, empty hash)
      perl -wle 'use Data::Dumper; my %a; @a{()} = (); print Dumper(\%a)'
        Ok, so I misspoke. It seems that in that particular context the empty list is being upgraded to an empty string, which I guess would count as a bug.
        update: Seeing how the example is pretty dumb, I wouldn't count it as much of a bug :)(its probably trivial to fix, but its just so dumb it wouldn't matter if its never fixed) At least this does the right thing :
        perl -wle 'use Data::Dumper; my %a; @a{my @b,1} = (); print Dumper(\%a +)' perl -wle 'use Data::Dumper; my %a; @a{my @b,1} = (1,2); print Dumper( +\%a)'

        MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
        I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
        ** The third rule of perl club is a statement of fact: pod is sexy.