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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |