| Variable-Alias documentation | view source | Contained in the Variable-Alias distribution. |
Variable::Alias - Alias any variable to any other variable
use Variable::Alias 'alias';
my $src;
my $a;
our $b;
my @c;
our @d;
alias $src => $a;
alias $a => $b;
alias $b => $c[0];
alias @c => @d;
$src='src';
# All the other variables now have the string
# 'src' in the appropriate places.
There are various ways to alias one variable to another in Perl. The most
popular is by assigning to typeglobs. This is quite efective, but only
works with globals. Another method is to use a module like
Lexical::Alias or Devel::LexAlias, but as their names suggest, these
only work with lexicals. There's no way to alias an element of an array
or hash.
Variable::Alias changes all that. It uses a tie to provide One True Way
to alias a variable.
Variable::Alias may export any or all of five functions. If you've used
Lexical::Alias, the interface is virtually identical.
alias(VAR, VAR)alias takes two variables of any type (scalar, array or hash) and
aliases them. Make sure they have the sigil you want on the front.
This function is only available in Perl 5.8.0 and later, because the prototype tricks it uses were first implemented in that version.
alias_s(SCALAR, SCALAR)alias_s takes two scalars and aliases them.
alias_a(ARRAY, ARRAY)alias_a takes two arrays and aliases them. Note that this is actual
arrays, not array elements, although you can alias references in
elements, like so:
alias_a(@short, @{$some->sequence->{of}->calls->{that's}[2]{long}});
alias_h(HASH, HASH)alias_h takes two hashes and aliases them.
alias_r(REF, REF)alias_r takes two references and aliases them. The referents must be
of the same type.
If at some point you want to break the alias, just say untie $variable.
(The variable will not retain the value it had while aliased.)
Alias must be of the same type as the originalYou tried to alias a variable of one type to a variable of another (e.g.
alias($scalar, @array)). You can't do that.
Can't alias type %sYou tried to alias a subroutine or a glob or something. This module only handles scalars, arrays, and hashes; everything else you might want to alias lives in a typeglob anyway.
Type of arg %s to %s must be %s (not %s)This mouthful is generated by Perl; it means that you used e.g. a scalar
with alias_a. Make sure you're using the right alias_X variant for
the type you're aliasing, and that Variable::Alias can actually handle
the type in question.
Typeglobs and Filehandles in perldata, Devel::LexAlias, and
Lexical::Alias for information on other aliasing methods.
perltie for information on tying.
This'll be slow, because tied variables always are. Blech. If your
aliasing needs are fairly simple, consider just using Lexical::Alias
or typeglobs--they'll be faster.
If you find any other bugs, drop me a note at <brentdax@cpan.org>.
Brent Dax <brentdax@cpan.org>
Copyright (C) 2002 Brent Dax <brentdax@cpan.org>. All Rights Reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| Variable-Alias documentation | view source | Contained in the Variable-Alias distribution. |