Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic


by Anonymous Monk
on Jan 19, 2001 at 01:45 UTC ( #52849=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a subroutine that needs to take two parameters, a scalar and an array. If I call and define it like this:
&func( $scalar, @array ); sub func { my $var = shift; my @list = @_; # do stuff }

Will $var have the value of $scalar and @list the value of @array?

If not, I had considered passing in the parameters as a scalar and a reference to an array. That would go like this

&func ( $scalar, \@array ); sub func { my $var = shift; my @list = ???? #what the heck do I put here to get @list == @array? # do stuff

Am I correctly passing an array reference into func? And once I do so, how do I get it out again?

Thanks in advance.


Replies are listed 'Best First'.
Re: parameters
by chipmunk (Parson) on Jan 19, 2001 at 01:51 UTC
    Yep, your first code snippet will work just fine. (Did you try it out? :)

    In your second code snippet, you can get the array back with either of:

    my @list = @{ $_[0] }; my @list = @{shift()};
    Passing a ref to the array, as in your second snippet, is the way to go if you need to pass in several arrays and keep them distinct in the subroutine.
(jeffa) Re: parameters
by jeffa (Bishop) on Jan 19, 2001 at 01:53 UTC
    Yes, always pass an array as a reference when passing multiple arguments to a subroutine.

    You de-reference the array in the sub like so:

    my ($var,$rlist) = @_; # de-reference $rlist like so: @{$rlist} - but, TIMTOWTDI ;) foreach(@{$rlist}) { #do stuff }
    Almost forgot about this one:
    my $var = shift; my @list = @{+shift}; # you cannot use @{shift}, the + operator tricks perl # into getting the value of shift first


    (the triplet paradiddle)
Re: parameters
by $code or die (Deacon) on Jan 19, 2001 at 02:45 UTC
    The first example will work, but as chipmunk says, you should use a reference if you are passing one or more arrays\hashes to a sub, otherwise it gets very difficult. Consider this:
    my @fruit = ('apples', 'pears', 'bananas'); my @drinks = ('cola', 'lemonade', 'water'); foo(@fruit,@drinks);
    This is not a big problem if the number of entities in @fruit and @drinks is always the same, but suppose there are different amounts of fruit or drinks each time the script runs?

    With a reference you can do this:
    foo(\@fruit,\@drinks); sub foo { my ($fruit,$drinks) = @_; print $fruit->[0], "\n"; print $drinks->[0], "\n"; }
    This will print:

    References are really useful but a bit daunting at first. Check perlref documentation and dive in and see what happens.

    $code or die
    Using perl at
    The Spiders Web
Re: parameters
by Anonymous Monk on Jan 19, 2001 at 20:37 UTC
    The wonderful folks at perlmonks come through again. Thank you very much for all your help.

    P.S. In case you were wondering why I asked about the first snippet (which was easily testable, as you pointed out), it just seemed a little hack-ish and I wanted to make sure there weren't going to be any unforseen consequences. The real question I wanted to ask was the second one, which seemed a much cleaner implementation (especially if I need to pass in a second array).

    Thanks again for all of your help, everyone.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://52849]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2021-03-04 00:45 GMT
Find Nodes?
    Voting Booth?
    My favorite kind of desktop background is:

    Results (97 votes). Check out past polls.