Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
No such thing as a small change
 
PerlMonks  

Re: passing variables between subroutines

by Grygonos (Chaplain)
on Jan 16, 2006 at 10:42 UTC ( [id://523523]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to passing variables between subroutines

foo("test","test2"); sub foo { my ($param_one,$param_two) = @_; print $param_one . "-".$param_two."\n"; }
you pull them off the @_ array you can shift them off the array individually or assign them in one shot as shown above. <edit> alternatively, if you have only one parameter you can do the following my $param_one = shift @_</edit>

Replies are listed 'Best First'.
Re^2: passing variables between subroutines
by bunnyman (Hermit) on Jan 16, 2006 at 16:17 UTC

    You don't need to do anything different if you have only one parameter.

    my ($param) = @_;

    Remember, you have to use the () there to create list context. Otherwise the @_ will be in scalar context (i.e. number of items in the array).

    I prefer this style, because it looks like C, Java, Ruby, etc. It also looks more like math, e.g. f(x, y, z).

Re^2: passing variables between subroutines
by blazar (Canon) on Jan 17, 2006 at 05:33 UTC
    you pull them off the @_ array you can shift them off the array individually or assign them in one shot as shown above. <edit> alternatively, if you have only one parameter you can do the following my $param_one = shift @_</edit>

    As bunnyman correctly pointed out there's no real difference between the case of one and of many parameters respectively. What is to be said here is that:

    1. shift is customary when passing just one parameter or when "singling" out one, e.g. typically in OO:
      my $self=shift; my ($foo, $bar, $baz)=@_;
      even though
      my ($self $foo, $bar, $baz)=@_; # would be just as fine.
    2. @_ is the implicit topic of shift, so, while you generally shift @an_array, the common and recommendable idiom here is
      my $param_one=shift;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://523523]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.