in reply to list vs array context: odd error message...

 $hash{$1} = split /,/, $2;
Looking this up it says i was assigning to @_, but it doesn't look like that at all to me. That looks like assingment to a hash entry as an anonymous list.

Close, but no cigar. You were assigning an array (the @_) to a scalar, which translates to the number of elements in the array.

You want:
$hash{$1} = [split /,/, $2];

Which creates the anonymous arrayref you were mentioning, which lives quite happily as a scalar

Replies are listed 'Best First'.
Re: Re: list vs array context: odd error message...
by Hot Pastrami (Monk) on Dec 16, 2000 at 04:41 UTC
    So does my inital reponse not accomplish the same thing in another way? I didn't test it, but if memory serves, its effect is identical.

    Hot Pastrami
      It's not identical if there's already something in the hash element. It reuses any existing hashref pointer, and may even fail if there's an arrayref or something else there instead.

      That's why I prefer avoiding the @{$foo[bar]} = ... forms, opting instead for the $foo[bar] = [...] forms as being much safer.

      -- Randal L. Schwartz, Perl hacker

        Oh yes, I see. Thanks.

        Hot Pastrami
      It does indeed accomplish the same thing, but it obfuscates what you're doing. (You dereference your lvalue, while I make a references of the rvalue). As proof, I wasn't sure your way worked until I tested it. :)

      To each his own.