Contextual::Return 0.1.0
Sponsored Links
Contextual::Return 0.1.0 Ranking & Summary
File size:
0.022 MB
Platform:
Any Platform
License:
Perl Artistic License
Price:
Downloads:
1009
Date added:
2007-01-18
Publisher:
Damian Conway
Contextual::Return 0.1.0 description
Contextual::Return is a Perl module to create context-senstive return values.
SYNOPSIS
use Contextual::Return;
use Carp;
sub foo {
return
SCALAR { thirty-twelve }
BOOL { 1 }
NUM { 7*6 }
STR { forty-two }
LIST { 1,2,3 }
HASHREF { {name => foo, value => 99} }
ARRAYREF { [3,2,1] }
GLOBREF { *STDOUT }
CODEREF { croak "Dont use this result as code!"; }
;
}
# and later...
if (my $foo = foo()) {
for my $count (1..$foo) {
print "$count: $foo is:n"
. " array: @{$foo}n"
. " hash: $foo->{name} => $foo->{value}n"
;
}
print {$foo} $foo->();
}
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!} }
}
Contextual returns
Those three subroutines can also be used in another way: as labels on a series of contextual return blocks (collectively known as a context sequence). When a context sequence is returned, it automatically selects the appropriate contextual return block for the calling context. So the previous example could be written even more cleanly as:
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 { return @server_data{ qw(name uptime load users) } }
SCALAR { return $server_data{load} }
VOID { print "$server_data{load}n" }
DEFAULT { croak q{Bad context! No biscuit!} }
);
}
The context sequence automatically selects the appropriate block for each call context.
SYNOPSIS
use Contextual::Return;
use Carp;
sub foo {
return
SCALAR { thirty-twelve }
BOOL { 1 }
NUM { 7*6 }
STR { forty-two }
LIST { 1,2,3 }
HASHREF { {name => foo, value => 99} }
ARRAYREF { [3,2,1] }
GLOBREF { *STDOUT }
CODEREF { croak "Dont use this result as code!"; }
;
}
# and later...
if (my $foo = foo()) {
for my $count (1..$foo) {
print "$count: $foo is:n"
. " array: @{$foo}n"
. " hash: $foo->{name} => $foo->{value}n"
;
}
print {$foo} $foo->();
}
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!} }
}
Contextual returns
Those three subroutines can also be used in another way: as labels on a series of contextual return blocks (collectively known as a context sequence). When a context sequence is returned, it automatically selects the appropriate contextual return block for the calling context. So the previous example could be written even more cleanly as:
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 { return @server_data{ qw(name uptime load users) } }
SCALAR { return $server_data{load} }
VOID { print "$server_data{load}n" }
DEFAULT { croak q{Bad context! No biscuit!} }
);
}
The context sequence automatically selects the appropriate block for each call context.
Contextual::Return 0.1.0 Screenshot
Contextual::Return 0.1.0 Keywords
ID
Return 0.1.0
LIST
SCALAR
VOID
To Create
Return values
Perl module
server
return
data
context
load
Foo
Contextual::Return
ContextualReturn
Bookmark Contextual::Return 0.1.0
Contextual::Return 0.1.0 Copyright
WareSeeker periodically updates pricing and software information of Contextual::Return 0.1.0 full version from the publisher, so some information may be slightly out-of-date. You should confirm all information before relying on it. Software piracy is theft, Using crack, password, serial numbers, registration codes, key generators is illegal and prevent future development of Contextual::Return 0.1.0 Edition. Download links are directly from our publisher sites, torrent files or links from rapidshare.com, yousendit.com or megaupload.com are not allowed
Featured Software
Want to place your software product here?
Please contact us for consideration.
Contact WareSeeker.com
Related Information
contexto de durango
returning to work
server3
how to create a website
proxy server
tax return
context free grammar
return to sender
contextual advertising
server proxy
to create a website
dataquick
context clues
return on equity
load cells
server 2008
how to create a web page
return to mysterious island
Related Software
Contextual::Return::Failure is a Perl module with a utility module for Contextual::Return. Free Download
Convert::Scalar is a Perl module that can convert between different representations of perl scalars. Free Download
Test::Data is a Perl module to test functions for particular variable types. Free Download
ferrisfuse is a FUSE module for mounting libferris filesystems through the Linux kernel. Free Download
Config::FreeForm is a Perl module that provides in-memory configuration data. Free Download
Test::Trap is a Perl module with trap exit codes, exceptions, output, etc. Free Download
HTTunnel is a universal HTTP tunnel using Apache, mod_perl and Perl. Free Download
TclMilter is a package for Tcl that implements an interface to Sendmails Milter API. Free Download
Latest Software
Popular Software
Favourite Software