Update: I'm blind, can't read. (Ignore the contents of this post other than to -- it.). I, as jd points out, completely screwed this one up.

This is just a scoping issue. If you have a lexical at the outer scope and you declare a lexical at the inner scope (as in your example) the inner scope only sees the new version not the old. As you did the aliasing at the outer level and not the inner, the inner lexical isn't aliased. The outer will still be so once you return.

You could either, not declare the inner lexicals of the same name, OR alias the inner copy of $alias to $orig, and you would get the results you are expecting.

Like halley, I'm not quite sure why you would want to do this. $_[0] is already an alias for the first parameter passed. If that parameter is a reference, then using it as an lvalue will always affect the thing it refers to.

If the idea is to give a meaningful name to the parameter without copying the reference (a very cheap operation anyway) then you could go with defining a constant

use constant BYREF=>0; sub something{ $_[BYREF] = 1000 if $_[BYREF] > 1000; ... }

The win is minimal unless the sub is being called zillions of times, but using a defined CONSTANT cost nothing at runtime and is much cheaper than allocating a lexical and then calling a sub to alias it to $_[0], which is much more expensive that just assigning the ref to an local-scope lexical.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

In reply to Re: Lexical::Alias & subroutines by BrowserUk
in thread Lexical::Alias & subroutines by Jenda

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.