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...


In reply to YARQ:Yet another reference question by snafu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.