If I understand you correctly, you are asking why would someone pass an array or hash reference to a subroutine.

There are two reasons I can think of immediately: passing multiple distinct arrays/hashes to a subroutine and passing an array or hash that you want to manipulate in the subroutine.

If you pass multiple arrays into a subroutine, there's no way to distinguish where one ends and the next begins. That's why you would pass them as references.

If your subroutine adds or removes elements from the hash or array and you want that to be reflected when the subroutine ends, you must pass by reference.

Edit: Here's some code to show what I mean.

#!/usr/bin/perl use warnings; use strict; my @a = qw( a1 a2 a3 ); my @b = qw( b1 b2 b3 ); my %c = ( a=>1, b=>2 ); my %d = ( c=>3, d=>4 ); sub multi_flat { # my ( @foo1, @foo2 ) = what should go here? # my ( @foo1, @foo2 ) = ( shift, shift ) ? # my ( @foo1, @foo2 ) = ( $_[0], $_[1] ) ? my @foo = @_; #this flattens the arrays into one print join( ", ", @foo), "\n"; } multi_flat( @a, @b ); #prints "a1, a2, a3, b1, b2, b3" multi_flat( %c, %d ); #prints "a, 1, b, 2, c, 3, d, 4" sub multi_fixed_arr { my ( $arr1, $arr2 ) = @_; print "\$arr1 is ", join( ", ", @$arr1), "\n"; print "\$arr2 is ", join( ", ", @$arr2), "\n"; } sub multi_fixed_hash { my ( $hash1, $hash2 ) = @_; print "keys \$hash1 is ", join( ", ", keys %$hash1), "\n"; print "keys \$hash2 is ", join( ", ", keys %$hash2), "\n"; } multi_fixed_arr( \@a, \@b ); #prints: $foo1 is a1, a2, a3 # $foo2 is b1, b2, b3 multi_fixed_hash( \%c, \%d ); #prints: keys $foo1 is a, b # keys $foo2 is c, d sub pop_one { my @arr1 = @_; pop @arr1; } print join( ", ", @a), "\n"; #prints "a1, a2, a3" pop_one( @a ); print join( ", ", @a), "\n"; #prints "a1, a2, a3" sub pop_one_fixed { my $arr = shift; pop @$arr; } print join( ", ", @a), "\n"; #prints "a1, a2, a3" pop_one_fixed( \@a ); print join( ", ", @a), "\n"; #prints "a1, a2"

In reply to Re: Array or Hash Reference to Subroutine by AR
in thread How to use @EXPORT by pkperl

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.