Subroutinen in Perl

In Returns and Perl Subroutines wird ein interessantes CPAN Modul beschreiebn:

The Contextual::Return CPAN module provides a mechanism by which you can specify that a subroutine returns different scalar values in boolean, numeric, string, hash-ref, array-ref, and code-ref contexts.


use Contextual::Return;

 sub get_server_status {

 my ($server_ID) = @_;

 # Acquire server data somehow...

 my %server_data = _ascertain_server_status($server_ID);

 # Return different components of that data, depending on call context...

 return (

 LIST { @server_data{ qw( name uptime load users ) }; }

 BOOL { $server_data{uptime} > 0; }

 NUM { $server_data{load};

 STR { "$server_data{name}: $server_data{uptime}, $server_data{load}"; }

 HASHREF { \%server_data; }

 );

 }

Possibly related posts (automatically generated)