Package Info

perl-Contextual-Return


Create context-sensitive return values


Development/Libraries/Perl

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) = @_;


    my %server_data = _ascertain_server_status($server_ID);



    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) = @_;


    my %server_data = _ascertain_server_status($server_ID);



    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!}                 }
}

License: Artistic-1.0 or GPL-1.0+
URL: http://search.cpan.org/dist/Contextual-Return/

Categories

Releases

Package Version Update ID Released Package Hub Version Platforms Subpackages
0.004014-bp156.3.1 info GA Release 2023-07-22 15 SP6
  • AArch64
  • ppc64le
  • s390x
  • x86-64
  • perl-Contextual-Return
0.004014-bp155.2.8 info GA Release 2023-05-17 15 SP5
  • AArch64
  • ppc64le
  • s390x
  • x86-64
  • perl-Contextual-Return
0.004014-bp154.1.15 info GA Release 2022-05-09 15 SP4
  • AArch64
  • ppc64le
  • s390x
  • x86-64
  • perl-Contextual-Return
0.004014-bp153.1.12 info GA Release 2021-03-06 15 SP3
  • AArch64
  • ppc64le
  • s390x
  • x86-64
  • perl-Contextual-Return
0.004014-bp152.3.12 info GA Release 2020-04-16 15 SP2
  • AArch64
  • ppc64le
  • s390x
  • x86-64
  • perl-Contextual-Return
0.004014-bp151.3.1 info GA Release 2019-07-18 15 SP1
  • AArch64
  • ppc64le
  • s390x
  • x86-64
  • perl-Contextual-Return
0.004014-bp151.2.10 info GA Release 2019-05-18 15 SP1
  • AArch64
  • ppc64le
  • s390x
  • x86-64
  • perl-Contextual-Return
0.004014-bp150.2.4 info GA Release 2018-07-30 15
  • AArch64
  • ppc64le
  • s390x
  • x86-64
  • perl-Contextual-Return