Lexicals seem very different to me from anything there's in C, am I wrong?
A lexicals usually behaves like a stack variable. However, that breaks down when closures are used (e.g. do { my $lex; sub { return \$lex; } }), or when the address of the lexical is copied outside of the lexical's scope (e.g. my @a; { my $lex; push(@a, \$lex); } and do { my $lex; \$lex }).
A lexical always behaves like a ref-counted pointer (stack variable) to dynamically allocated memory (heap variable). In other words,
my $scalar;
is something like
ref_ptr<scalar_t> scalar = new scalar_t();
Is there any way, in Perl, to create an alias to a lexical?
Yes:
our $sym; local *sym = \$lexical;
However, I think you meant to ask if there was a way to alias a lexical to something else. I think
for my $lexical ($var)
is the only way provided by the language, but there might be other ways interally. If so, there might be an XS module that does this.
In reply to Re: Finally, C
by ikegami
in thread Finally, C
by Ido
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |