Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Passing variables into a subroutine

by CountZero (Bishop)
on Sep 03, 2012 at 19:31 UTC ( #991506=note: print w/ replies, xml ) Need Help??


in reply to Passing variables into a subroutine

Your prototype definition is wrong. Your definition of (@) says that your sub takes one array or list as argument, so all arguments are flattened into one list. This is the way any "normal" subroutine without prototypes works.

However now consider this:

use Modern::Perl; sub create_output (\@$) { my ($arrayref_of_lines, $entry_no_new) = @_; say "array: @{$arrayref_of_lines}"; say "scalar: $entry_no_new"; } my @array = qw/1 2 3 4/; create_output @array, 5;
And its output:
array: 1 2 3 4 scalar: 5

This is actually one of the few cases of a prototype being applied in a useful way.

The prototype (\@$) means the first argument to your sub MUST start with a @ (in other words, it must be an array), followed by a scalar. Due to the (\@) in the prototype, Perl actually takes a reference to the array and passes that as the first argument to your sub. So don't you forget to de-reference it in your sub!

It is this automagical referencing that allows you to pass one or more arrays to a subroutine without them all getting flattened in one big @_.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics


Comment on Re: Passing variables into a subroutine
Select or Download Code
Re^2: Passing variables into a subroutine
by Freezer (Sexton) on Sep 04, 2012 at 10:26 UTC
    Perl actually takes a reference to the array and passes that as the first argument to your sub. So don't you forget to de-reference it in your sub!



    Is there a popper way to do the dereferencing, given the context?
      Sadly, there will be no automatic dereferencing done by Perl. But it is very easy to do: to get the original array, just add the @ sigil to the front of the scalar holding the reference.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      My blog: Imperial Deltronics
Re^2: Passing variables into a subroutine
by Freezer (Sexton) on Sep 04, 2012 at 10:29 UTC
    Is something like the following equally cool?
    sub look_through_file ($$) { my ($start_point, $continue_tag) = @_;
      As a general rule don't use prototypes unless there is a good reason for it such as passing in several arrays without them getting flattened into one @_. And even then passing references to the arrays is probably a more clear solution.

      I see no merit in adding a prototype definition of ($$).

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      My blog: Imperial Deltronics

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://991506]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (13)
As of 2014-08-01 13:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (20 votes), past polls