Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Predefining sub Parameters

by jimbojones (Friar)
on Jun 22, 2005 at 17:28 UTC ( #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


Comment on Re: Predefining sub Parameters
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (11)
As of 2014-09-18 16:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (118 votes), past polls