File Coverage

blib/lib/Hash/Abbrev.pm
Criterion Covered Total %
statement 22 22 100.0
branch 6 6 100.0
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 35 35 100.0


line stmt bran cond sub pod time code
1             package Hash::Abbrev;
2 2     2   51992 use Text::Abbrev ();
  2         102  
  2         50  
3 2     2   2686 use Hash::Util 'hv_store';
  2         5523  
  2         12  
4 2     2   211 use warnings;
  2         9  
  2         63  
5 2     2   12 use strict;
  2         4  
  2         528  
6            
7             sub abbrev {
8 5 100   5 1 34723 my $hash = ref $_[0] eq 'HASH' ? shift : {};
9 5         33 my $abbrev = Text::Abbrev::abbrev keys %$hash, @_;
10 5         357 for (keys %$abbrev) {
11 57 100       120 next if exists $$hash{$_};
12 46         58 my $full = $$abbrev{$_};
13 46 100       170 hv_store %$hash, $_, exists $$hash{$full}
14             ? $$hash{$full}
15             :($$hash{$full} = $full)
16             }
17             $hash
18 5         25 }
19            
20             sub import {
21 2     2   28 require Exporter;
22 2         3 goto &{Exporter->can('import')}
  2         145  
23             }
24            
25             our @EXPORT = 'abbrev';
26             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 function by default.
63            
64             =head1 SUBROUTINES
65            
66             =head2 C
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
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<< >>
112            
113             =head1 BUGS
114            
115             please report any bugs or feature requests to C,
116             or through the web interface at
117             L. 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 for the abbreviation table.
126            
127             =item L for C.
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             __PACKAGE__ if 'first require'