Create context-senstive return values

Edit Package perl-Contextual-Return
http://search.cpan.org/dist/Contextual-Return

Usually, when you need to create a subroutine that returns different values
in different contexts (list, scalar, or void), you write something like:

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...
if (wantarray()) {
return @server_data{ qw(name uptime load users) };
}
if (defined wantarray()) {
return $server_data{load};
}
if (!defined wantarray()) {
carp 'Useless use of get_server_status() in void context';
return;
}
else {
croak q{Bad context! No biscuit!};
}
}

That works okay, but the code could certainly be more readable. In its
simplest usage, this module makes that code more readable by providing
three subroutines--'LIST()', 'SCALAR()', 'VOID()'--that are true only when
the current subroutine is called in the corresponding context:

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...
if (LIST) { return @server_data{ qw(name uptime load users) } }
if (SCALAR) { return $server_data{load} }
if (VOID) { print "$server_data{load}\n" }
else { croak q{Bad context! No biscuit!} }
}

Refresh
Refresh
Source Files
Filename Size Changed
Build.PL 0000000479 479 Bytes
Contextual-Return-0.004007.tar.gz 0000039900 39 KB
perl-Contextual-Return.changes 0000002638 2.58 KB
perl-Contextual-Return.spec 0000003578 3.49 KB
Revision 11 (latest revision is 17)
Stephan Kulow's avatar Stephan Kulow (coolo) accepted request 146109 from Stephan Kulow's avatar Stephan Kulow (coolo) (revision 11)
- update to 0.004007
  - Further patch to failures.t
  - Added redefinition of Scalar::Util::blessed() to avoid nasty
    surprises (thanks Andrew!)
  - Added confess() and cluck() overrides
    and fixed caller() override to set @DB::args when appropriate
  - Further patch for t/confess.t
    (Thanks, David!)
  - Circumvented annoying undef-as-key warnings in Perl 5.17+
  - Honour @CARP_NOT's when reporting context
  - Added BLESSED handler for better control over how blessed() lies
  - Upgraded reimplementation of blessed() to make more sense
- reanable tests for ARM (fixed upstream)
- fix ppc test failures
- provide Build.PL to be able to use create_packlist=0 (forwarded request 145673 from k0da)
Comments 0
openSUSE Build Service is sponsored by