Chapter 3
Add a few more optional parameters, and pretty soon the average call is using
more undefs than values!
Contrast this to the call with named parameters:
check_mail(imap_sever => $imap_server,
username => $username,
password => $password,
retries => 3);
Named parameter subroutines will automatically treat missing keys as undef so
there's no need to put them in just for padding.
Return Values
If you need to return multiple values, return a list or a reference to an array. Avoid
the tendency to slip into C mode and make your users pass in output parameters
by reference. For example, the hobnob() subroutine sends a message to the BOA
satellite and retrieves a response. It has two values to return a status flag indi
cating success or failure and the received message. In C mode, it might look like
the following:
sub hobnob {
my ($message, $reply_ref) = @_;
my $status = _send_message($message);
$$reply_ref = _get_message();
return $status;
}
It would be called like this:
$reply = "";
$status = hobnob("Hello out there!", \$reply);
The $reply variable is passed by reference and the retrieved message is filled in.
A simpler and more Perl ish way to do the same thing is to simply return a list
of two values:
sub hobnob {
my ($message) = @_;
my $status = _send_message($message);
my $reply = _get_message();
return ($status, $reply);
}
78
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved