If one has a reference and one wants to treat it like a regular variable then one cheap trick is to do this

$Href = {1=>1, 2=>2}; *H = $Href; # aliases the referenced variable as a real variable print $H{1};

But that's sort of evil, since one is actually creating a global variable %H not a lexical or local. If one has "use strict" turned on it will complain unless one declares "our %H";

So the question, oh great experts, is if it is possible to get this aliasing effect but with a lexical variable. Conceptually, I'd like to be able to write this:

use strict; sub foo { my $Href = shift; my (%H); *H = $Href; # define the alais print $H{1}; }

But that does not work.

Something that does work, but once again uses globals, is to create a global, and then scope the alias inside the function using "local" like this:

use strict; our %H; # have to create the global or local complains sub foo { my $Href = shift; local (%H); # instead of my use local *H = $Href; # define the alias print $H{1}; }

But now we are back to using global variables again which I don't like. Is there anyway to get an alias to the referenced variable that uses the lexical, scoped variables??

Code tags added by GrandFather


In reply to are TypeGlob assingments to a lexical variables possible? by cems22

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.