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>

Grygonos

Replies are listed 'Best First'.
Re^2: passing variables between subroutines
by bunnyman (Hermit) on Jan 16, 2006 at 21: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 10: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;