Does Perl Always Pass Argument By Reference?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Akino877
    New Member
    • Nov 2007
    • 37

    Does Perl Always Pass Argument By Reference?

    Hello,

    I read on one web page that Perl always passes its arguments by reference.
    But on another web page, it was said that Perl can pass its argument by
    value :
    Code:
       printHash(%hash);
    Could someone please tell me if the second web page is correct?

    Thank you - Akino
  • Kelicula
    Recognized Expert New Member
    • Jul 2007
    • 176

    #2
    You need to be clearer as to where you are talking about perl passing things to. Do you mean mean passing arguments to a subroutine? A hash in perl is really an array, (not really but when it comes to passing things) if you pass a named hash to a subroutine (in certain context) it will be "flattened" into an array where the first element is the "key" and the second is the "value" and so on... as in:
    %hash = ( name => 'Jim', age => '29');
    will become:
    (name, Jim, age, 29);
    When passed...

    Comment

    • Kelicula
      Recognized Expert New Member
      • Jul 2007
      • 176

      #3
      If you pass an anonymous hash it will be treated as a reference.
      eg:
      $hash = { name => 'Jim', age => '29'};

      Comment

      • Akino877
        New Member
        • Nov 2007
        • 37

        #4
        Hi Kelicula,

        Thank you very much for your reply.

        Akino

        Comment

        • Kelicula
          Recognized Expert New Member
          • Jul 2007
          • 176

          #5
          When passing to subroutines, the convention is; if there are more than 3-4 things you are passing you should probably be using an anonymous hash, otherwise you can just pass the value directly.
          eg:
          Code:
          sub passing_things {
              my $thing = shift;
              do stuff with thing passed...
          }
          # Then later...
          passing_things($variable);
          The value in $variable will be passed to the subroutine.
          However if you find you are sending a lot of things you can just use an anonymous hash:

          Code:
          sub passing_more_things {
              my $things = shift;
              do many things with lots of stuff...
          }
          # then later...
          passing_more_things({ one => $var, two => $var2, three => $var3, four => $var4});
          
          # If you want to you can pass different kinds of things explicitly:
          passing_more_things($variable, { one => $var, two => $var2}, @array);
          In that case all that stuff will be "flattened" into an anonymous array "@_" inside the sub:
          Code:
          sub big_passing {
              my @all_the_stuff = @_;
          }
          # Then later
          my $var = 'hello';
          my @array = ('perl', 'is', 'very', 'flexible');
          
          big_passing($var, @array, { kelicula => 'loves', all => 'things_perl'});
          the contents of @all_the_stuff would be:
          hello, perl, is, very, flexible, kelicula, loves, all, things_perl.

          Get it?

          Comment

          • Akino877
            New Member
            • Nov 2007
            • 37

            #6
            Yes, I understand it now. Thank you.

            Comment

            Working...