File Coverage

blib/lib/Scalar/IfDefined.pm
Criterion Covered Total %
statement 17 17 100.0
branch 2 2 100.0
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 26 26 100.0


line stmt bran cond sub pod time code
1             package Scalar::IfDefined;
2              
3 4     4   327732 use 5.006;
  4         15  
4 4     4   22 use strict;
  4         9  
  4         91  
5 4     4   21 use warnings;
  4         9  
  4         138  
6              
7 4     4   20 use Scalar::Util qw/blessed reftype/;
  4         9  
  4         393  
8              
9 4     4   20 use Exporter 'import';
  4         8  
  4         1631  
10             our @EXPORT_OK = qw/ifdef $ifdef/;
11              
12              
13             our $VERSION = '0.08';
14              
15              
16             sub ifdef(&$) {
17 2     2 1 601 my ($block, $scalar) = @_;
18              
19 2 100       12 return $scalar if not defined $scalar;
20 1         5 return $block->($scalar) for $scalar;
21             }
22              
23              
24             our $ifdef = sub {
25             my $obj = shift;
26             my ($method, @args) = @_;
27              
28             return undef if not defined $obj;
29              
30             return $obj->$method(@args) if blessed $obj;
31            
32             my $methtype = reftype($method);
33             return $obj->$method(@args) if defined($methtype) and $methtype eq 'CODE';
34            
35             return $obj->[$method] if reftype $obj eq 'ARRAY';
36             return $obj->{$method} if reftype $obj eq 'HASH';
37             return $obj->($method, @args) if reftype $obj eq 'CODE';
38              
39             die "Can't getdef on " . reftype $obj;
40             };
41              
42              
43              
44             1; # End of Scalar::IfDefined
45              
46             __END__