NAME
Scalar::IfDefined - Apply block to scalar depending on if it's defined.
VERSION
Version 0.04
SYNOPSIS
use Scalar::IfDefined qw/ifdef/;
my $hash = {
a => 1,
b => 2,
c => 3,
d => {
E => 1,
F => 2,
},
};
ifdef { $_ + 1 } $hash->{a}; # ---> 2
ifdef { $_ + 1 } $hash->{missing}; # ---> undef
ifdef { $_ + 1 } ifdef { $_->{F} } $hash->{d}; # ---> 3
ifdef { $_ + 1 } ifdef { $_->{MISSING} } $hash->{d}; # ---> undef
# Or perhaps with Perl6::Flows
use Perl6::Flows;
$hash->{a}
==> ifdef { $_->{F} }
==> ifdef { $_ + 1 }; # ---> 3
EXPORT
- ifdef
SUBROUTINES/METHODS
ifdef
Takes a block and a scalar value.
If the scalar value is undef, the block is ignored and undef is returned straight away.
If the scalar value is defined, then the block is evaluated with $_ as the value passed in, and the result of the block is returned.
AUTHOR
Nick Booker, <NMBooker at gmail.com>
BUGS
https://github.com/nmbooker/p5-Scalar-IfDefined/issues
ACKNOWLEDGEMENTS
LICENSE AND COPYRIGHT
Copyright 2014 Nick Booker.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.