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

I'm trying to learn referencing and am getting stumped on how to pass references out of subroutines. I found a lot of "search" info about passing references to subroutines but not back out.

My broken example code:

my ($r_array1, $r_array2) = foo(); print "@$r_array1\n"; print "@$r_array2\n"; sub foo { my @array1 = [1..5]; my @array2 = [6..10]; return (\@array1, \@array2); }

The above code results in a printout of the memory locations of the data and not the data itself. TIA!

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar";
$nysus = $PM . $MCF;

Replies are listed 'Best First'.
Re: Passing references out of subroutines
by Beatnik (Parson) on May 22, 2001 at 21:02 UTC
    That's because you're actually creating references to references to arrays with my @array1 = [1..5];...
    try :
    ($r_array1, $r_array2) = foo(); print "@$r_array1\n"; print "@$r_array2\n"; sub foo { my @array1 = (1..5); my @array2 = (6..10); return (\@array1, \@array2); }

    Update: while we're recommending books on the subject... Damian Conway's Object Orientated Perl is really one of my favorites...

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
      Ha! I'm a dummy...got referencing on the brain and confusing it with how to create an array! Thanks!

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar";
      $nysus = $PM . $MCF;

Re (tilly) 1: Passing references out of subroutines
by tilly (Archbishop) on May 22, 2001 at 21:07 UTC
    You are creating two levels of references. One when you use the anonymous array constructor. Again when you take references to arrays with only one thing apiece. You only dereference once.

    What you are doing is symptomatic of something I would call, "Mental flailing". Step back. Visualize your data structure. Try to produce it. (Use Data::Dumper to see if you really are creating what you think you should be.) Then try to access that. Take it one step at a time and it will work, but you have to trust that.

Re: Passing references out of subroutines
by andye (Curate) on May 22, 2001 at 21:20 UTC
    Wotcha. Some different approaches:
    my $r_array = [1,2,3]; # make an array and put a reference to it in $r +_array. - note square brackets my @array = (1,2,3); #make an array called @array - note normal bracke +ts $r_array = \@array; #then put a reference to @array in $r_array sub foo {return [1,2,3]) #this sub returns a reference to an array $r_array = foo(); #you could call it like this
    A good way to find out what's going on if references start to get confusing is to use the module Data::Dumper, like this:
    use Data::Dumper; print Dumper($r_array1, $r_array2); which prints out $VAR1 = [ [ 1, 2, 3, 4, 5 ] ]; $VAR2 = [ [ 6, 7, 8, 9, 10 ] ];
    and that shows you that $r_array1 actually contains a reference to a reference to an array (because there are two levels of square brackets).

    The best explanation of reference syntax is (imho) in Effective Perl Programming. You might also find perlref useful, if you haven't already looked at it.

    andy.

      You might also find perlref useful

      And also perlreftut (by our very own Dominus) for a kinder, gentler introduction.

      --
      <http://www.dave.org.uk>

      "Perl makes the fun jobs fun
      and the boring jobs bearable" - me

Re: Passing references out of subroutines
by princepawn (Parson) on May 22, 2001 at 21:16 UTC
    Maybe an old copy of "Advanced Perl Programming" by Sriram Srinivasan can be gotten for dirt cheap as Perl 6 is on it's way in... that was a great book to learn references from. Prince "sorry, but you can't have my copy" Pawn