ITworld.com
  Search  
ITworld Home Page ITworld Webcasts ITworld White Papers ITworld Newsletters ITworld News ITworld Topics Careers ITworld Voices ITwhirled Changing the way you view IT
Named Parameters for Subroutine Calls
PERL --- 07/26/2001

Andrew Johnson

Perl doesn't have typed or named parameters for subroutines, all parameters are passed in the @_ array as a flat list. However, since a list can also be a hash, we can fake it and create subroutines that can be called like: 

On this topic

my $status = login( -username => $user,
-password => $pass, -host => $host, );

This assumes we have already obtained values for $user and $pass from the user input or the command line (or wherever), and we have a function named login() that apparently is going to log us in to $host. Without worrying about the specifics of the function itself, let's look at how it can process the arguments:

sub login {
my %args = @_; # do stuff }

Easy right, so what benefit do we get? First, our calling code is rather self-documenting (even if we chose horrible names for the variables themselves, the hash keys give it all away). Second, we no longer have to worry about the order we give the arguments, and neither does our subroutine! We can call it like above, or like either of these:

my $status = login( -password => $pass,
-host => $host, -username => $user, );

my $status = login( -password => $pass,
-username => $user, -host => $host, );

As a bonus, if we want a subroutine where some or all of the arguments are optional (in any order), then we can do that too:

sub draw_rect3D {
my %args = shift; $args{-length} ||= 1; $args{-width} ||= 1; $args{-height} ||= 1; $args{-units} = $args{-units} eq 'in'? 'in' : 'cm'; # get to work }

Now, we can call this and supply any of the parameters, all of the parameters, or none of the parameters (the default will be a 1 centimeter cube):

draw_rect3D(-length => 2, -units => 'in'); # 2 x 1 x 1 inches draw_rect3D(-length => 5, -wdith => 2); # 5 x 2 x 1 centimeters draw_rect3D(-length => 5, -height => 10, -units => 'in', -width => 3, ); # 5 x 3 x 10 inches

This is quite unlike our regular way of just using the argument array as an array -- in that case, the order you use must always be the same, and optional arguments must be at the end.

You will see this style of function used often for Object constructor methods (and other OO methods) and it is widely used in the various Tk routines and methods (for GUI programming). However, you can use this technique with any regular subroutine -- all we are doing is using the argument list as a hash instead of a list or array.

 

Andrew Johnson works as a programmer/consultant and is the author of Elements of Programming with Perl from Manning Publications.



Advertisements
Sponsored links
Locate Hidden Software on business PCs with this free tool
KODAK i1400 Series Scanners stand up to the challenge
Top 5 Reasons to Combine App Performance and Security
Bring harmony to your mix of UNIX-Linux-Windows computing environments
 Home   Newsletters  PERL
www.itworld.com    open.itworld.com     security.itworld.com     smallbusiness.itworld.com
storage.itworld.com     utilitycomputing.itworld.com     wireless.itworld.com

 
Contact Us   About Us   Privacy Policy    Terms of Service   Reprints  

CIO   Computerworld   CSO   GamePro   Games.net   Industry Standard   Infoworld   ITworld  
JavaWorld   LinuxWorld  MacUser   Macworld   Network World   PC World   Playlist  

DEMO   IDG Connect   IDG Knowledge Hub   IDG TechNetwork   IDG World Expo  

Copyright © Computerworld, Inc. All rights reserved

Reproduction in whole or in part in any form or medium without express written permission of Computerworld Inc. is prohibited. Computerworld and Computerworld.com and the respective logos are trademarks of International Data Group Inc.