Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Predefining sub Parameters

by jimbojones (Friar)
on Jun 22, 2005 at 17:28 UTC ( [id://469106]=note: print w/replies, xml ) Need Help??


in reply to Predefining sub Parameters

Hi

You're kinda stuck with the fact that all parameters to your sub are going to be passed in through @_. You can make the assignment the first thing you do in the sub, either by shifting or assigning.
sub functionname { my $string_param = shift; my $other_string_param = shift; my @array_param = @_ ; #-- the remnant after the first two are assign +ed #-- do edit check here, throw error #-- do rest of sub function }
or all at once
sub functionname { my ( $string_param, $other_string_param, @array_param ) = @_; #-- do edit check here, throw error #-- do rest of sub function }
note that if you want to pass in a mix of strings and an array, the array has to come last, as everything gets 'flattened' onto @_. If you want to pass in two or more arrays, or an array in the middle of your list, you need to use a reference.
sub functionname { my $string_param = shift; my $array_ref = shift; my @array = @{$array_ref}; my $other_string_param = shift; #-- do edit check here, throw error #-- do rest of sub function } #-- call as: sub functionname( $string_param, \@array_param, $other_string_param);
Finally, stay away from sub prototypes. Search the monastery for all the reasons why.

- j

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (8)
As of 2024-04-18 08:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found