Also sprach Michele Dondi:
Speaking of which, may I ask you to give an explanation of how this
works internally? (Just curious...)
It probably means that on the inside just the pointer to the original
variable is copied. On ordinary assignments, perl copies the C structure
into a new variable. Here it just copies the memory address.
What I mean is that we all know that Perl5 has basically two
fundamentally orthogonal variable systems. And we know we can access
directly a package's symbol table, but AFAIK we can't do the same with
lexical variables, even though there "MUST" be one, and given the
orthogonality hinted to above, it is somewhat surprising that such an
aliasing can even be done. Oh, and of course on lexical scope exit it
will "downgrade" to a simple copy, won't it? (Just tried, and AFAICT
it is indeed so).
No, an alias remains an alias. Perl increments the reference-count of
the variable to be aliased:
ethan@ethan:~$ perl -MDevel:

eek
{ my $c = 42;
Dump($c);
*alias = \$c;
Dump($alias);
}
Dump($alias);
__END__
SV = IV(0x8163094) at 0x814cc6c
REFCNT = 1
FLAGS = (PADBUSY,PADMY,IOK,pIOK)
IV = 42
SV = IV(0x8163094) at 0x814cc6c
REFCNT = 2
FLAGS = (PADBUSY,PADMY,IOK,pIOK)
IV = 42
SV = IV(0x8163094) at 0x814cc6c
REFCNT = 1
FLAGS = (PADBUSY,PADMY,IOK,pIOK)
IV = 42
So after doing the aliasing, the reference-count has gone up to 2. On
scope exist, it is decreased again by one. Since the reference-count
hasn't yet gone to zero, the memory previously associated with 'my $c'
remains intact and can still be accessed through the alias. Also note
how the memory addresses involved are all identical. My suspicion is
that aliases were just a side-effect that turned out to be very useful.
It is the C-semantics of copying a pointer ported to perl.
Aliases are a lot like hard links on Unices. You can delete the file
that was hard-linked to but the hard link itself remains valid and
behaves if it was the original file. Which it is anyway, from the point
of view of the filesystem.
As for lexical variables, technically they are not very different from
package variables. They have the PADMY flag set which means they are
subject to refcounting. Other than that, they only differ from package
variables in that they are not kept in a symbol-table but in so called
pads. Each block has its own pad and they are inaccessible from
pure-Perl (unless someone writes an XS extension that makes them
accessible).
Tassilo