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

I've just picked up Perl (a couple of days), but by the looks of it I'm missing something obvious. I've spent hours going through many threads, but just can't figure this out:
use warnings; use strict; sub testfunc(); { my $temp2=testfunc(); my @arr=@$temp2; print @arr, $temp2," \n" ; #want @arr to print the array elements & $temp2 to print ref value } sub testfunc() { my @temps= [1,2,3]; return \@temps; }
Output: ARRAY<0X37ce48>Array<0X4eaaa0>

Expected Output: 123Array<0X4eaaa0>

Replies are listed 'Best First'.
Re: Why isn't the obvious dereferencing of the array working?
by toolic (Bishop) on Apr 29, 2015 at 18:05 UTC
    Square brackets construct a reference to an array. Parens construct a list. Change:
    my @temps= [1,2,3];

    to:

    my @temps= (1,2,3);

Re: Why isn't the obvious dereferencing of the array working?
by AnomalousMonk (Archbishop) on Apr 29, 2015 at 18:13 UTC
Re: Why isn't the obvious dereferencing of the array working?
by Laurent_R (Canon) on Apr 29, 2015 at 19:11 UTC
    Just in case you don't know, the prototypes, in the case of your code, are the empty parentheses after the function declaration.

    In addition, if you don't add these parens, you don't need to pre-declare your subroutine. So, your code could be:

    use warnings; use strict; { my $temp2=testfunc(); my @arr=@$temp2; print @arr, $temp2," \n" ; #want @arr to print the array elements & $temp2 to print ref value } sub testfunc { my @temps= (1,2,3); return \@temps; }
    But it could also be:
    use warnings; use strict; { my $temp2=testfunc(); my @arr=@$temp2; print @arr, $temp2," \n" ; #want @arr to print the array elements & $temp2 to print ref value } sub testfunc { my $temps= [1,2,3]; return $temps; }

    Je suis Charlie.