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

I want to be able to index into an array that is returned by a procedure so I try the following:
my $val; $val = &func[1]; print $val; sub func { qw ( just some list ); }

I get a compile error on the "$val = &func[1];" line.

Why doesn't this work? I know I can come up with several work arounds but this seems more elegant (except of course it's broken ;) )

Thanks,

Sizzle
Edited 2004-06-09 by Ovid

Replies are listed 'Best First'.
Re: index into list returned by subroutine
by diotalevi (Canon) on Jun 09, 2004 at 20:31 UTC
    $val = ( func() )[ 1 ]; $val = ( &func )[1];
Re: index into list returned by subroutine
by herveus (Prior) on Jun 09, 2004 at 20:33 UTC
    Howdy!

    my $val = (func())[1];

    Put the function call (with parens, and without ampersand) in parens, and index that list...

    yours,
    Michael
Re: index into list returned by subroutine
by Fletch (Bishop) on Jun 09, 2004 at 20:31 UTC

    It doesn't work because it's wrong. $val = ( func() ) [ 0 ] would give you the first element of the list returned. And read perldoc perlsub about the differences between func();, &func();, and &func;.

Re: index into list returned by subroutine
by Roy Johnson (Monsignor) on Jun 09, 2004 at 20:42 UTC
    Instead of rolling your own code delimiters, use the special <code></code> tag pair. That will keep PerlMonks from treating anything within them as special, like it did your [angle brackets]

    The PerlMonk tr/// Advocate