passing (and getting) references to/from functions

K

K4 Monk

I don't understand references very well (even after reading some docs
on this). How can I get a mixed array of hash and array back from a
function? How do I send it such a list of mixed types? Can someone
please help!

#!/usr/bin/perl
use strict;

sub func {
my @list;
push (@list, "map");
push (@list, "key");
push (@list, "l");
push (@list, "j");

my @arr;
push (@arr, "egg");
push (@arr, "hell");


return ( \@list, \@arr );

}

sub func {
my $pid=shift;
my $p=shift;
my $ref=shift;
print "$ref\n";
my %funcs=%{\$ref};
foreach my $key(keys %funcs) { print "$key\n"; }
}

my @arrref = &func();
my @l = @{$arrref[0]};
my @r = @{$arrref[1]};

print "keys\n";
foreach my $k(@l) { print "$k\n"; }
print "array\n";
foreach my $rr(@r) { print "$rr\n"; }

my $pid=10;
my $p=11;
my %hash;
$hash{"derp"} = 10;
$hash{"herp"} = 11;

&func($pid, $p, \(%hash));
 
S

sln

I don't understand references very well (even after reading some docs
on this). How can I get a mixed array of hash and array back from a
function? How do I send it such a list of mixed types? Can someone
please help!

$subref = sub { my @junkary;
my @mixed = ( # start of mixed array
[1,2,3,"string"], # element 0, a anonymous array ref
\@junkary, # element 1, a reference to a named array
{ # element 2, a anonymous hash ref
vals1 => 'hello',
vals2 => [ # key val2 element = anonymous array ref
'a','b','c'
],
},
# ... # element 3 ...
); # end of mixed array
return \@mixed;
# or, return @mixed;
}

$mixedref = $subref->();
print $mixedref->[2]->{vals2}->[1];
#or $mixedref->[2]{vals2}[1] but I would test this
__END__
b

#!/usr/bin/perl
use strict;

sub func {
my @list;
push (@list, "map");
push (@list, "key");
push (@list, "l");
push (@list, "j");

my @arr;
push (@arr, "egg");
push (@arr, "hell");


return ( \@list, \@arr );

}

sub func {
my $pid=shift;
my $p=shift;
my $ref=shift;
print "$ref\n";
my %funcs=%{\$ref};
foreach my $key(keys %funcs) { print "$key\n"; }
}

^^^^^^
I don't think subs can be overloaded in Perl, so these would need
different names.

my @arrref = &func();
my @l = @{$arrref[0]};
my @r = @{$arrref[1]};

This is ok, you are dereferencing the array refs.
print "keys\n";
foreach my $k(@l) { print "$k\n"; }
print "array\n";
foreach my $rr(@r) { print "$rr\n"; }

Use some proper style here,
for () {
...
}
elsif () {
...
}
else {
...
}
my $pid=10;
my $p=11;
my %hash;
$hash{"derp"} = 10;
$hash{"herp"} = 11;

&func($pid, $p, \(%hash));
^
Drop the ampersand. There is a use for it but its more just
a carryover from legacy Perl, otherwise the called sees @_

-sln
 
X

Xho Jingleheimerschmidt

K4 said:
I don't understand references very well (even after reading some docs
on this). How can I get a mixed array of hash and array back from a
function? How do I send it such a list of mixed types? Can someone
please help!

#!/usr/bin/perl
use strict;

You should also "use warnings;"

sub func { ....
sub func {

You should use different names for different functions. If you turn on
warnings, it would tell you about that problem.

Beyond that, it is hard to figure out what you *expect* to happen, so it
is hard to explain why your expectations were not met.

Xho
 
J

Jürgen Exner

K4 Monk said:
I don't understand references very well (even after reading some docs
on this). How can I get a mixed array of hash and array back from a
function?

You can't. The return value of a function is always a flat list of
scalars. However of course you can return a list of references, each
reference pointing to whatever complex data structure you can envision.
How do I send it such a list of mixed types?

Same way as you return them from a sub.
#!/usr/bin/perl
use strict;

sub func {
my @list;
push (@list, "map");
push (@list, "key");
push (@list, "l");
push (@list, "j");

my @arr;
push (@arr, "egg");
push (@arr, "hell");


return ( \@list, \@arr );

This looks good to me. Where does the code doesn't do what you expect it
to do?
sub func {

????
Two subs with the same name?
I am getting
Subroutine func redefined at t.pl line 22.
my $pid=shift;
my $p=shift;
my $ref=shift;
print "$ref\n";
my %funcs=%{\$ref};
foreach my $key(keys %funcs) { print "$key\n"; }
}

my @arrref = &func();

Don't do that unless you know exactly why you have to do it. There is no
good reason to call a function with the &. You don't use prototypes
(nobody does), so why do you want to override them? Not to mention the
other side effects.

[more code snipped]

Please clearly state what
- you expect your code to do
- you actually observe your code doing
and -unless obvious- how those two are different

Otherwise it is impossible to tell where you are asking for help.

jue
 
K

K4 Monk

Beyond that, it is hard to figure out what you *expect* to happen, so it
is hard to explain why your expectations were not met.


hi sln, jue and Xho, thanks to all three of you for taking the time.
In my code
push (@list, "map");
push (@list, "key");
push (@list, "l");
push (@list, "j");
return ( \@list, \@arr );

I intended to use a hash %list which should have been

$list{"map"}="key";
$list{"l"}="j";

but because I couldn't figure out how to return it from a function, I
changed it into an array.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top