Re: (quickie) (Aliasing) Possible to get this to work in strict?
From: Tassilo v. Parseval (tassilo.von.parseval_at_rwth-aachen.de)
Date: 12/16/04
- Next message: Robin: "Re: Can't get past 'use strict' :("
- Previous message: brian d foy: "Re: FAQ 4.36: How can I expand variables in text strings?"
- In reply to: Michele Dondi: "Re: (quickie) (Aliasing) Possible to get this to work in strict?"
- Next in thread: Arndt Jonasson: "Re: (quickie) (Aliasing) Possible to get this to work in strict?"
- Reply: Arndt Jonasson: "Re: (quickie) (Aliasing) Possible to get this to work in strict?"
- Reply: Anno Siegel: "Re: (quickie) (Aliasing) Possible to get this to work in strict?"
- Reply: Michele Dondi: "Re: (quickie) (Aliasing) Possible to get this to work in strict?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 16 Dec 2004 10:03:13 +0100
Also sprach Michele Dondi:
> On Tue, 14 Dec 2004 09:54:16 +0100, "Tassilo v. Parseval"
><tassilo.von.parseval@rwth-aachen.de> wrote:
>
>>You can circumvent that by pre-declaring $r using our() or 'use vars':
>>
>> use strict;
>>
>> our $r; # pre-5.6.0 perls: use vars qw/$r/;
>> my $n = 5;
>>
>> *r = \$n;
>> $r++;
>> print "$n - $r\n";
>> __END__
>> 6 - 6
>
> 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::Peek
{ 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
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
- Next message: Robin: "Re: Can't get past 'use strict' :("
- Previous message: brian d foy: "Re: FAQ 4.36: How can I expand variables in text strings?"
- In reply to: Michele Dondi: "Re: (quickie) (Aliasing) Possible to get this to work in strict?"
- Next in thread: Arndt Jonasson: "Re: (quickie) (Aliasing) Possible to get this to work in strict?"
- Reply: Arndt Jonasson: "Re: (quickie) (Aliasing) Possible to get this to work in strict?"
- Reply: Anno Siegel: "Re: (quickie) (Aliasing) Possible to get this to work in strict?"
- Reply: Michele Dondi: "Re: (quickie) (Aliasing) Possible to get this to work in strict?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]