Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Small troubles with references

by wfsp (Abbot)
on Feb 09, 2006 at 20:12 UTC ( [id://529179]=note: print w/replies, xml ) Need Help??


in reply to Small troubles with references

Hi coldmiser!

I agree with you, references can be tricky beasties to get the hang of.

You successfully passed refs to your sub and de-referenced them. That's half the battle won already!

But you can, in fact, pass an array or a hash to a sub. Change your test script to test it.

The problem arises when, say, you want to pass two arrays.

pass_arrays(@array_one, @array_two); sub pass_arrays{ my @a1 = @_; my @a2 = @_; }
Perl will 'flatten' both arrays into one array and copy it into @a1. @a2 will be empty. If you pass refs:
pass_arrays(\@array_one, \@array_two); sub pass_arrays{ my ($a1ref, $a2ref) = @_; }
$a1ref and $a2ref now contain the refs you passed and you can use them as you did in your snippet.

You might also write a script that changes the array/hash passed into a sub and then look at what they look like after the sub has been called.

Your snippet also raises the question of scoping (as duff mentions). Have a look at Coping with Scoping to get more of an idea of what going on.

You're nearly there, good luck!

Replies are listed 'Best First'.
Re^2: Small troubles with references
by injunjoel (Priest) on Feb 09, 2006 at 21:00 UTC
    Greetings,
    Just to help illustrate how Perl 'flattens' or as I try to think of it (thanks to diotalevi) 'list-ifies' subroutine arguments consider the following
    #!/usr/bin/perl -w use strict; my @array = qw(arrayval1 arrayval2 arrayval3 arrayval4 arrayval5); my %hash = (key1=>'val1', key2=>'val2', key3=>'val3'); sub check_input_list { print "\n"; print "@_"; print "\n"; } check_input_list(@array, %hash); check_input_list(\@array, %hash); check_input_list(@array, \%hash); check_input_list(\@array, \%hash);
    The Output
    arrayval1 arrayval2 arrayval3 arrayval4 arrayval5 key2 val2 key1 val1 +key3 val3 ARRAY(0x1824320) key2 val2 key1 val1 key3 val3 arrayval1 arrayval2 arrayval3 arrayval4 arrayval5 HASH(0x182435c) ARRAY(0x1824320) HASH(0x182435c)
    Notice in the output of the first call the keys and values from %hash get tacked onto the end of the values in @array as if the entire thing were one huge list. Now imagine trying to get keys or values from %hash? Not likely nor easy.
    In the second and third examples we pass one reference but the argument list is still just a list.
    The final example is much cleaner. If you were confused about what you originally passed into the sub your could always call ref and find out; otherwise there is no way to tell if you are passed a 'list-ified' hash or array.

    -InjunJoel
    "I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://529179]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-26 08:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found