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

Fellow Monks and Monkettes:
my @temp = (2,3,4); $selects[0] = [@temp];
After the above setup,
$refdates = $selects[0]; @workdts = @$refdates;
works, but...
@workdts = @$selects[0];
...does not.

Why? I could use an enlightened bonk on the head. :)

Don Wilde
"There's more than one level to any answer."

Replies are listed 'Best First'.
Re: Directly Dereferencing an Array Reference to an Array
by cdarke (Prior) on Aug 30, 2006 at 17:49 UTC
    In a word: precedence. Perl is actually looking for a scalar called $selects, you need a touch of the old curlies:
    @workdts = @{$selects[0]}
    to force precedence your way.
Re: Directly Dereferencing an Array Reference to an Array
by Velaki (Chaplain) on Aug 30, 2006 at 17:52 UTC

    Turning on strict and warnings helps a boodle.

    Converting your code to

    #!/usr/bin/perl use strict; use warnings; my @temp = (2,3,4); my @selects; $selects[0] = [@temp]; my $refdates = $selects[0]; #my @workdts = @$refdates; my @workdts = @$selects[0]; print "$_\n" for @workdts;
    gives me
    Global symbol "$selects" requires explicit package name at ./p line...

    Now since we explicitly declared @selects as an array, then $selects[0] should be an array element, which it is, and we only need to cast that reference to an array type.

    Well, since @ binds tighter than subscripting, the compiler thought we were trying to use @$selects, which implies that $selects, the scalar, exists. Since it doesn't, we need to enforce what we want with braces.

    @{$selects[0]}
    This will give us the result we desire.

    Hope this helped,
    -v.

    "Perl. There is no substitute."
      Many thanks!
      Before I get heaped with an OD of ashes, the reason I don't have strict and warnings on is that this is embperl code embedded in a web page. Embperl does things (such as loading once with [! !]) which do not fare well under strict pragmas.

      Don Wilde
      "There's more than one level to any answer."