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.
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 @a1 = @_; my @a2 = @_; }
$a1ref and $a2ref now contain the refs you passed and you can use them as you did in your snippet.pass_arrays(\@array_one, \@array_two); sub pass_arrays{ my ($a1ref, $a2ref) = @_; }
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!
In reply to Re: Small troubles with references
by wfsp
in thread Small troubles with references
by coldmiser
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |