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

I have been spending the last couple of hours on learning more about references (more out of necessity rather than curiosity) and I just haven't been able to find an answer to the situation I am finding myself in.

if ( &check_for_valid_conf($config_file) ) { my (@class, @rules) = &parse_conf($config_file); print "class has $#class elements and rules has $#rules elements\n +"; print join("\n",@class),"\n"; } sub parse_conf { ... return(@class,@ind_rules); }
Ok. What we have here is a perfect example of why I need to use or would prefer to use references. This is good cuz when I started coding in Perl 6 months ago I couldn't even think of a reason why I would need to use references. But, finally, I have a reason now...and I am falling short of the best way to do it in this case.

Briefly, what I would like to do is pass the two arrays being returned from the sub routing to be two arrays in main::. That obviously can't be done the way it is written above because the return() will crunch the arrays into one array leaving me with only one array. :P

So, what would be the best way to pass those two arrays from the subroutine to the caller to return two arrays?

I've tried the following code to no avail. I admit, I am kind of shooting in the dark because I am still trying to grasp using references properly.

Here is some of the things I tried:

my ($arr1, $arr2) = \&sub(arg); print join("\n",@$arr1); print join("\n",@$arr2); sub sub { @arr1 = qw(foo bar quux); @arr2 = (1,2,3); return(\@arr1,\@arr2); } perl -Mstrict -e ' my ($arr1, $arr2) = \&sub("arg"); print join("\n",@$arr1); print join("\n",@$arr2); sub sub { my @arr1 = qw(foo bar quux); my @arr2 = (1,2,3); return(\@arr1,\@arr2); }' Not an ARRAY reference at -e line 3. perl -Mstrict -e ' my (@arr1, @arr2) = &sub("arg"); print join("\n",@arr1); print join("\n",@arr2); sub sub { my @arr1 = qw(foo bar quux); my @arr2 = (1,2,3); return(\@arr1,\@arr2); }' ARRAY(0x10f07c) ARRAY(0x10f0c4) perl -Mstrict -e ' my (@arr1, @arr2) = &sub("arg"); print join("\n",@{$arr1}),"\n\n"; print join("\n",@{$arr2}),"\n"; sub sub { my @arr1 = qw(foo bar quux); my @arr2 = (1,2,3); return(\@arr1,\@arr2); }' Global symbol "$arr1" requires explicit package name at -e line 3. Global symbol "$arr2" requires explicit package name at -e line 4. Execution of -e aborted due to compilation errors. perl -Mstrict -e ' my (@arr1, @arr2) = &sub("arg"); print join("\n",@arr1),"\n\n"; print join("\n",@arr2),"\n"; sub sub { my $arr1 = \qw(foo bar quux); my $arr2 = \(1,2,3); return($arr1,$arr2); }' SCALAR(0x10f0d0) SCALAR(0x10f100) perl -Mstrict -e ' my ($arr1, $arr2) = &sub("arg"); print join("\n",@$arr1),"\n\n"; print join("\n",@$arr2),"\n"; sub sub { my $arr1 = \qw(foo bar quux); my $arr2 = \(1,2,3); return($arr1,$arr2); }' Not an ARRAY reference at -e line 3. perl -Mstrict -e ' print &sub("arg"),"\n"; sub sub { my $arr1 = \qw(foo bar quux); my $arr2 = \(1,2,3); return($arr1,$arr2); }' SCALAR(0x1061d8)SCALAR(0x10613c) perl -Mstrict -e ' print @{&sub("arg")},"\n"; sub sub { my $arr1 = \qw(foo bar quux); my $arr2 = \(1,2,3); return($arr1,$arr2); }' Not an ARRAY reference at -e line 2. perl -Mstrict -e ' print ${&sub("arg")},"\n"; sub sub { my $arr1 = \qw(foo bar quux); my $arr2 = \(1,2,3); return($arr1,$arr2); }' 3 OOH, getting closer! :) perl -Mstrict -e ' print @{&sub("arg")},"\n"; sub sub { my @arr1 = qw(foo bar quux); my @arr2 = (1,2,3); return(\@arr1,\@arr2); }' 123 GETTING CLOSER YET! :) perl -Mstrict -e ' my (@arr1,@arr2) = @{&sub("arg")}; print join(", ",@arr1),"\n\n";print join(", ",@arr2),"\n"; sub sub { my @arr1 = qw(foo bar quux); my @arr2 = (1,2,3); return(\@arr1,\@arr2); }' 1, 2, 3
Hmm, and I am still having problems. So, I can get the second array out now. How come the first one isn't getting out? :)

Please be gentle, fellow monks :]

_ _ _ _ _ _ _ _ _ _
- Jim
Insert clever comment here...

Replies are listed 'Best First'.
Re: YARQ:Yet another reference question
by derby (Abbot) on Mar 13, 2002 at 19:35 UTC
    so close,

    my ($arr1, $arr2) = &sub(); print join("\n",@$arr1); print join("\n",@$arr2); sub sub { my @arr1 = qw(foo bar quux); my @arr2 = (1,2,3); return(\@arr1,\@arr2); }

    note: you don't have to my the arrays in your function, it's just nicer to not have globals hanging around (also, I'm not to crazy about a subroutine named "sub" - perfectly legal but a little too wacky).

    -derby

Re: YARQ:Yet another reference question
by PrimeLord (Pilgrim) on Mar 13, 2002 at 19:36 UTC
    You can return the arrays as ref's for us in main like this.

    my ($arr1,$arr2) = &sub(); sub { return \(@arr1, @arr2); }


    And then you can access the arrays with:

    $arr1->[0] $arr2->[1] etc.

    HTH

    -Prime

    Update:Beaten to the punch again :)
Re: YARQ:Yet another reference question
by particle (Vicar) on Mar 13, 2002 at 19:37 UTC
    you show great initiative by doing so much to understand references. with this in mind, i thought rather than coding an answer for you i'd point you to the docs! enjoy ;)

    ~Particle

Re: YARQ:Yet another reference question
by Juerd (Abbot) on Mar 13, 2002 at 19:55 UTC

    Good luck, and have fun reading.

    U28geW91IGNhbiBhbGwgcm90MTMgY
    W5kIHBhY2soKS4gQnV0IGRvIHlvdS
    ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
    geW91IHNlZSBpdD8gIC0tIEp1ZXJk