File Coverage

blib/lib/Hash/Identity.pm
Criterion Covered Total %
statement 15 15 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 20 20 100.0


line stmt bran cond sub pod time code
1             package Hash::Identity;
2              
3 2     2   80843 use warnings;
  2         6  
  2         104  
4 2     2   20 use strict;
  2         5  
  2         296  
5              
6             =head1 NAME
7              
8             Hash::Identity - Get a hash that always returns the key
9              
10             =head1 VERSION
11              
12             Version 0.01
13              
14             =cut
15              
16             our $VERSION = '0.01';
17              
18              
19             =head1 SYNOPSIS
20              
21             use Hash::Identity qw(e);
22              
23             print "The result is: $e{ 1 + 2 }\n";
24             print "sin(1) = $e{ sin(1) }\n";
25              
26             # Or you wish to import multiple. though I think one is usually sufficient.
27              
28             use Hash::Identity qw(ident expr);
29              
30             print "You could use expr like this: $expr{2**3}.\n";
31             print "Or you could use ident $ident{ 'a' . 'b' } as well.\n";
32              
33             # NOTE
34              
35             use Hash::Identity qw(e);
36             print "If you want to just call a sub without params. Don't use $e{ rand }. Use $e{ rand() } instead.\n";
37              
38             =head1 DESCRIPTION
39              
40             To get a hash that always returns the key.
41              
42             A hash that always returns the key is useful when interpolating EXPR in a double quoted string.
43              
44             This module uses L to achieve this,
45             and provides a better importing interface.
46              
47             =head1 EXPORT
48              
49             This module will not export anything by default.
50             You could assign each name you want to use like this:
51              
52             use Hash::Identity qw(a b c);
53              
54             Then you will have identity hash %a, %b, and %c.
55              
56             =cut
57              
58 2     2   3043 use Tie::Hash::Identity;
  2         610  
  2         120  
59              
60             tie our %e, 'Tie::Hash::Identity';
61              
62             sub import {
63 2     2   17 shift;
64 2     2   11 no strict 'refs';
  2         7  
  2         276  
65 2         15 *{caller()."::$_"} = \%e for @_;
  2         6251  
66             }
67              
68             =head1 SEE ALSO
69              
70             L
71              
72             =head1 AUTHOR
73              
74             Cindy Wang (CindyLinz)
75              
76             =head1 BUGS
77              
78             Please report any bugs or feature requests to C, or through
79             the web interface at L. I will be notified, and then you'll
80             automatically be notified of progress on your bug as I make changes.
81              
82              
83              
84              
85             =head1 LICENSE AND COPYRIGHT
86              
87             Copyright 2010 Cindy Wang (CindyLinz).
88              
89             This program is free software; you can redistribute it and/or modify it
90             under the terms of either: the GNU General Public License as published
91             by the Free Software Foundation; or the Artistic License.
92              
93             See http://dev.perl.org/licenses/ for more information.
94              
95              
96             =cut
97              
98             1; # End of Hash::Identity