blib/lib/Eponymous/Hash.pm

filter: Statementstmt 100% 20 / 20 Branchbran 100% 4 / 4 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 4 / 4 Podpod 100% 1 / 1 totaltotal 100% 32 / 32 CC 4 SCAR 7.6
CC
4
Cov
100%
eponymous_hash
3.0
import
2.0
BEGIN
1.0
CRAP 2.1
↑ Summary
1 package Eponymous::Hash;
2 1 ▶use PadWalker 'var_name';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '0.02';
6  
7 4 ▶sub import {
Subroutine import: called CC=2 SCAR=6.9
8 4 shift;
9 4 ▶ my $name = pop || 'eponymous_hash';
Condition100%2/2
pop() || 'eponymous_hash'
l!l
31
MC/DC100%1/1
pop() || 'eponymous_hash'
pop()
Truth table100%2/2
A
A= pop()
Aresult
11
00
10 4 my $calling_class = caller;
11  
12 4 *{"${calling_class}::$name"} = \&eponymous_hash;
13 }
14  
15 4 ▶sub eponymous_hash {
Subroutine eponymous_hash: called CC=3 SCAR=11.0
Pod documented
16 4 ▶ if (ref $_[0] eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 1 3 4
elsif (builtin::blessed($_[0])) { } 1 2 3
17 1 return map {$_ => $_[0]->{$_}} @_[1..$#_];
18 }
19 elsif (blessed $_[0]) {
20 1 return map {$_ => $_[0]->$_} @_[1..$#_];
21 }
22 else {
23 2 return map { substr(var_name(1, \$_), 1) => $_ } @_;
24 }
25 }
26  
27 1 1;
28  
29 =head1 NAME
30  
31 Eponymous::Hash - Translates named variables to a hash list with corresponding keys
32  
33 =head1 DESCRIPTION
34  
35 Translates named variables to a hash list with corresponding keys
36  
37 =head1 USAGE
38  
39 use Eponymous::Hash 'epy';
40  
41 The name 'epy' is arbitrary. You may define any name in the use statement.
42  
43 B<With scalars>
44  
45 my $mammal = 'ponycorn';
46 my $diet = 'sprinkles';
47  
48 my %hash = epy($mammal, $diet)
49 # (mammal => 'ponycorn', diet => 'sprinkles')
50  
51  
52 B<With hash reference>
53  
54 my $thing = {
55 mammal => 'ponycorns',
56 diet => 'sprinkls'
57 };
58  
59 my %hash = epy($thing, 'mammal', 'diet');
60 # (mammal => 'ponycorn', diet => 'sprinkles')
61  
62  
63 B<With blessed object>
64  
65 my $thing = Thing->new;
66 $thing->mammal; # ponycorns
67 $thing->diet; # sprinkles
68  
69 my %hash = epy($thing, 'mammal', 'diet');
70 # (mammal => 'ponycorn', diet => 'sprinkles')
71  
72 =head1 METHODS
73  
74 =head2 eponymous_hash
75  
76 Default method name. If parameter is passed to use statement, parameter will be used instead.
77  
78 =head1 VERSION
79  
80 0.02
81  
82 =head1 AUTHOR
83  
84 Glen Hinkle C<tempire@cpan.org>
↑ Summary