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.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); }
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:
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? :)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
Please be gentle, fellow monks :]
_
_
_
_
_
_
_
_
_
_
- Jim
Insert clever comment here...
In reply to YARQ:Yet another reference question by snafu
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |