Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are two ways you can do this: </p> <ol> <li>by prototype</li> <li>by reference</li> </ol> <p>But before I discuss these--if what you show in your question is about the extent of what you want to do--let me suggest <a href="http://search.cpan.org/perldoc?List::MoreUtils#pairwise" rel="nofollow noreferrer"><code>List::MoreUtils::pairwise</code></a></p> <p>So, where you would write this:</p> <pre><code>my @sum = two_array_sum( @a, @b ) </code></pre> <p>You'd simply write this: </p> <pre><code>my @sum = pairwise { $a + $b } @a, @b; </code></pre> <h3>By prototype</h3> <p>This works like <code>push</code>. (And just like <code>push</code> it demands to have a <code>@</code> sigil on <em>something</em>)</p> <pre><code>sub two_array_sub (\@\@) { my ( $aref, $bref ) = @_; ... } </code></pre> <p>That way when you do this</p> <pre><code>two_array_sub( @a, @b ); </code></pre> <p>it works. Whereas normally it would just show up in your sub as one long list. They aren't for everybody as you'll see in my discussion below.</p> <h3>By reference</h3> <p>That's the way that everybody is showing you. </p> <pre><code>some_sub( \@a, \@b ); </code></pre> <h3>About prototypes</h3> <p>They're finicky. This won't work if you have refs:</p> <pre><code>two_array_sub( $arr_ref, $brr_ref ); </code></pre> <p>You have to pass them like this:</p> <pre><code>two_array_sub( @$arr_ref, @$brr_ref ); </code></pre> <p>However, because making "array expressions" gets <em>really</em> ugly quickly with arrays nested deep, I often avoid Perl's fussiness as you can overload the type of reference Perl will take by putting it in a "character class" construct. <code>\[$@]</code> means that the reference can either be a scalar or array.</p> <pre><code>sub new_two_array_sub (\[$@]\[$@]) { my $ref = shift; my $arr = ref( $ref ) eq 'ARRAY' ? $ref : $$ref; # ref -&gt; 'REF'; $ref = shift; my $brr = ref( $ref ) eq 'ARRAY' ? $ref : $$ref; ... } </code></pre> <p>So all these work: </p> <pre><code>new_two_array_sub( @a, $self-&gt;{a_level}{an_array} ); new_two_array_sub( $arr, @b ); new_two_array_sub( @a, @b ); new_two_array_sub( $arr, $self-&gt;{a_level}{an_array} ); </code></pre> <p>However, Perl is still fussy about this... for some reason:</p> <pre><code>new_two_array_sub( \@a, $b ); OR new_two_array_sub( $a, [ 1..3 ] ); </code></pre> <p>Or any other "constructor" that still could be seen as a reference to an array. Fortunately, you can shut Perl up about that with the old <em>Perl 4</em> <strong><code>&amp;</code></strong></p> <pre><code>&amp;new_two_array_sub( \@a, [ 1..3 ] ); </code></pre> <p>Then the mux-ing code in the sub takes care of handling two array references.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload