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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.