File Coverage

blib/lib/Variable/Alias.pm
Criterion Covered Total %
statement 63 81 77.7
branch 4 8 50.0
condition n/a
subroutine 21 24 87.5
pod 5 6 83.3
total 93 119 78.1


line stmt bran cond sub pod time code
1             package Variable::Alias; {
2 2     2   50676 use 5.6.0;
  2         7  
  2         104  
3 2     2   11 use strict;
  2         4  
  2         72  
4 2     2   11 use warnings;
  2         8  
  2         100  
5            
6 2     2   20 use Exporter;
  2         4  
  2         104  
7 2     2   2174 use Switch 'Perl6';
  2         122751  
  2         11  
8 2     2   182689 use Carp 'croak';
  2         6  
  2         1024  
9            
10             our @ISA=qw(Exporter);
11             our @EXPORT_OK=qw(alias alias_s alias_a alias_h alias_r);
12             our $VERSION=0.01;
13            
14             sub export_fail {
15             #This can only be called because of an
16             # insufficient version for alias();
17            
18 0     0 0 0 die sprintf("Perl version %vd doesn't support features needed for alias()", $^V);
19             }
20            
21             sub alias_r {
22 8     8 1 33 my($src, $dest)=@_;
23            
24 8 50       29 croak "Alias must be of the same type as the original"
25             unless ref $dest eq ref $src;
26            
27 8         10 given(ref $dest) {
  8         12  
  8         35  
  0         0  
28 8 100       123 when('SCALAR') {
  6         87  
29 6         48 tie($$dest, 'Variable::Alias::Scalar', $src);
30 6         44 }
  0         0  
  0         0  
  0         0  
31            
32 2 50       30 when('ARRAY') {
  2         26  
33 2         35 tie(@$dest, 'Variable::Alias::Array', $src);
34 2         14 }
  0         0  
  0         0  
  0         0  
35            
36 0 0       0 when('HASH') {
  0         0  
37 0         0 tie(%$dest, 'Variable::Alias::Hash', $src);
38 0         0 }
  0         0  
  0         0  
  0         0  
39            
40             else {
41 0         0 croak "Can't alias type ", ref $src;
42             }
43             }
44             }
45            
46             sub alias_s(\$\$) {
47 3     3 1 27 goto &alias_r;
48             }
49            
50             sub alias_a(\@\@) {
51 1     1 1 9 goto &alias_r;
52             }
53            
54             sub alias_h(\%\%) {
55 0     0 1 0 goto &alias_r;
56             }
57            
58             if($^V ge 5.8.0) {
59 4     4 1 151 eval q{
60             sub alias(\[$@%]\[$@%]) {
61             goto &alias_r;
62             }
63             }
64             }
65             else {
66             our @EXPORT_FAIL=qw(alias);
67             }
68             }
69            
70             package Variable::Alias::Scalar; {
71 2     2   16 use strict;
  2         4  
  2         76  
72 2     2   19 use warnings;
  2         4  
  2         153  
73            
74 2     2   2618 use Tie::Scalar;
  2         1405  
  2         193  
75             our @ISA=qw(Tie::StdScalar);
76            
77             sub TIESCALAR {
78 6     6   23 return bless $_[1], $_[0];
79             }
80             }
81            
82             package Variable::Alias::Array; {
83 2     2   26 use strict;
  2         4  
  2         72  
84 2     2   11 use warnings;
  2         4  
  2         54  
85            
86 2     2   2020 use Tie::Array;
  2         2772  
  2         158  
87             our @ISA=qw(Tie::StdArray);
88            
89             sub TIEARRAY {
90 2     2   9 return bless $_[1], $_[0];
91             }
92             }
93            
94             package Variable::Alias::Hash; {
95 2     2   11 use strict;
  2         4  
  2         58  
96 2     2   19 use warnings;
  2         4  
  2         48  
97            
98 2     2   2076 use Tie::Hash;
  2         1919  
  2         183  
99             our @ISA=qw(Tie::StdHash);
100            
101             sub TIEHASH {
102 0     0     return bless $_[1], $_[0];
103             }
104             }
105            
106             =head1 TITLE
107            
108             Variable::Alias - Alias any variable to any other variable
109            
110             =head1 SYNOPSIS
111            
112             use Variable::Alias 'alias';
113             my $src;
114             my $a;
115             our $b;
116             my @c;
117             our @d;
118            
119             alias $src => $a;
120             alias $a => $b;
121             alias $b => $c[0];
122             alias @c => @d;
123            
124             $src='src';
125             # All the other variables now have the string
126             # 'src' in the appropriate places.
127            
128             =head1 DESCRIPTION
129            
130             There are various ways to alias one variable to another in Perl. The most
131             popular is by assigning to typeglobs. This is quite efective, but only
132             works with globals. Another method is to use a module like
133             C or C, but as their names suggest, these
134             only work with lexicals. There's no way to alias an element of an array
135             or hash.
136            
137             C changes all that. It uses a tie to provide One True Way
138             to alias a variable.
139            
140             =head2 Interface
141            
142             C may export any or all of five functions. If you've used
143             C, the interface is virtually identical.
144            
145             =over 4
146            
147             =item C(VAR, VAR)
148            
149             C takes two variables of any type (scalar, array or hash) and
150             aliases them. Make sure they have the sigil you want on the front.
151            
152             This function is only available in Perl 5.8.0 and later, because the
153             prototype tricks it uses were first implemented in that version.
154            
155             =item C(SCALAR, SCALAR)
156            
157             C takes two scalars and aliases them.
158            
159             =item C(ARRAY, ARRAY)
160            
161             C takes two arrays and aliases them. Note that this is actual
162             I, not array I, although you can alias references in
163             elements, like so:
164            
165             alias_a(@short, @{$some->sequence->{of}->calls->{that's}[2]{long}});
166            
167             =item C(HASH, HASH)
168            
169             C takes two hashes and aliases them.
170            
171             =item C(REF, REF)
172            
173             C takes two references and aliases them. The referents must be
174             of the same type.
175            
176             =back
177            
178             =head2 Breaking aliases
179            
180             If at some point you want to break the alias, just say C.
181             (The variable will not retain the value it had while aliased.)
182            
183             =head1 DIAGNOSTICS
184            
185             =over 4
186            
187             =item C
188            
189             You tried to alias a variable of one type to a variable of another (e.g.
190             C). You can't I that.
191            
192             =item C
193            
194             You tried to alias a subroutine or a glob or something. This module only
195             handles scalars, arrays, and hashes; everything else you might want to
196             alias lives in a typeglob anyway.
197            
198             =item C
199            
200             This mouthful is generated by Perl; it means that you used e.g. a scalar
201             with C. Make sure you're using the right C variant for
202             the type you're aliasing, and that C can actually handle
203             the type in question.
204            
205             =back
206            
207             =head1 SEE ALSO
208            
209             L, C, and
210             C for information on other aliasing methods.
211            
212             L for information on tying.
213            
214             =head1 BUGS
215            
216             This'll be slow, because tied variables always are. Blech. If your
217             aliasing needs are fairly simple, consider just using C
218             or typeglobs--they'll be faster.
219            
220             If you find any other bugs, drop me a note at .
221            
222             =head1 AUTHOR
223            
224             Brent Dax
225            
226             =head1 COPYRIGHT
227            
228             Copyright (C) 2002 Brent Dax . All Rights Reserved.
229            
230             This program is free software; you can redistribute it and/or modify it
231             under the same terms as Perl itself.