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!


In reply to Re: Small troubles with references by wfsp
in thread Small troubles with references by coldmiser

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.