blib/lib/Hash/Abbrev.pm

filter: Statementstmt 100% 25 / 25 Branchbran 100% 6 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 100% 38 / 38 CC 5 SCAR 11.0
CC
5
Cov
100%
abbrev
5.0
BEGIN
1.0
BEGIN
1.0
CRAP 3.0
↑ Summary
1 package Hash::Abbrev;
2 3 ▶ use Text::Abbrev ();
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶ use Hash::Util 'hv_store';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 5 ▶ sub abbrev {
Subroutine abbrev: called CC=5 SCAR=16.1
Pod documented
8 5 ▶ my $hash = ref $_[0] eq 'HASH' ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 3 2 5
9 5 my $abbrev = Text::Abbrev::abbrev keys %$hash, @_;
10 5 for (keys %$abbrev) {
11 57 ▶ next if exists $$hash{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash->{$_} 12 45 57
12 45 my $full = $$abbrev{$_};
13 hv_store %$hash, $_, exists $$hash{$full}
14 ? $$hash{$full}
15 45 ▶ :($$hash{$full} = $full)
Branch: 1 branch
BranchTrueFalseTotal
exists $hash->{$full} ? : 38 7 45
16 }
17 $hash
18 5 }
19
20 2 ▶ sub import {
Subroutine import: called CC=1 SCAR=0.0
21 2 require Exporter;
22 2 goto &{Exporter->can('import')}
23 }
24
25 3 our @EXPORT = 'abbrev';
26 3 our $VERSION = '0.01';
27
28 =head1 NAME
29
30 Hash::Abbrev - Text::Abbrev with aliases
31
32 =head1 VERSION
33
34 version 0.01
35
36 =head1 SYNOPSIS
37
38 this module creates an abbreviation hash where each abbreviation of the key is
39 read/write aliased to the same value.
40
41 use Hash::Abbrev;
42
43 my $hash = abbrev qw(file directory count);
44
45 say $$hash{f}; # 'file'
46 say $$hash{dir} # 'directory'
47
48 $_ .= '!' for @$hash{qw/f d c/};
49
50 say $$hash{file}; # 'file!'
51 say $$hash{co}; # 'count!'
52
53 or as a dispatch table:
54
55 @$hash{qw/file dir count/} = (\&load_file, \&read_dir, \&get_count);
56
57 $$hash{f}(...) # calls load_file(...)
58 $$hash{directory}(...) # calls read_dir(...)
59
60 =head1 EXPORT
61
62 this module exports the C<abbrev> function by default.
63
64 =head1 SUBROUTINES
65
66 =head2 C<abbrev LIST>
67
68 takes a list of strings and returns a hash reference where all of the
69 non-ambiguous abbreviations are aliased together. the returned reference is to
70 an ordinary hash, it is not tied or magic in any way.
71
72 the behavior could be written out this way if the C< := > operator meant 'alias
73 the lhs to the rhs':
74
75 abbrev 'abc', 'xyz' ~~ $h{abc} = 'abc'
76 $h{ab} := $h{abc}
77 $h{a} := $h{abc}
78 $h{xyz} = 'xyz'
79 $h{xy} := $h{xyz}
80 $h{x} := $h{xyz}
81
82 =head2 C<abbrev HASHREF LIST>
83
84 the first argument to C< abbrev > can be a hash reference. that hash will be
85 modified in place with the existing keys and values and then will be returned.
86 an additional list of keys to abbreviate can be provided after the hash
87 reference.
88
89 my $hash = abbrev {
90 file => sub {"file(@_)"},
91 directory => sub {"directory(@_)"},
92 };
93
94 say $$hash{f}('abc.txt'); # 'file(abc.txt)'
95 say $$hash{dir}('/'); # 'directory(/)'
96
97 since the modification is done in place, the following also works:
98
99 my %hash = (
100 file => sub {"file(@_)"},
101 directory => sub {"directory(@_)"},
102 );
103
104 abbrev \%hash;
105
106 say $hash{f}('abc.txt'); # 'file(abc.txt)'
107 say $hash{dir}('/'); # 'directory(/)'
108
109 =head1 AUTHOR
110
111 Eric Strom, C<< <asg at cpan.org> >>
112
113 =head1 BUGS
114
115 please report any bugs or feature requests to C<bug-hash-abbrev at rt.cpan.org>,
116 or through the web interface at
117 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Hash-Abbrev>. I will be
118 notified, and then you'll automatically be notified of progress on your bug as I
119 make changes.
120
121 =head1 ACKNOWLEDGEMENTS
122
123 =over 4
124
125 =item L<Text::Abbrev> for the abbreviation table.
126
127 =item L<Hash::Util> for C<hv_store>.
128
129 =back
130
131 =head1 LICENSE AND COPYRIGHT
132
133 copyright 2011 Eric Strom.
134
135 this program is free software; you can redistribute it and/or modify it under
136 the terms of either: the GNU General Public License as published by the Free
137 Software Foundation; or the Artistic License.
138
139 see http://dev.perl.org/licenses/ for more information.
140
141 =cut
142
143 3 __PACKAGE__ if 'first require'
↑ Summary