cems22 has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: are TypeGlob assingments to a lexical variables possible?
by davido (Cardinal) on Sep 25, 2006 at 06:26 UTC | |
by cems22 (Initiate) on Sep 25, 2006 at 13:55 UTC | |
|
Re: are TypeGlob assingments to a lexical variables possible?
by diotalevi (Canon) on Sep 25, 2006 at 14:12 UTC | |
|
Re: are TypeGlob assingments to a lexical variables possible?
by ikegami (Patriarch) on Sep 25, 2006 at 15:45 UTC | |
by cems22 (Initiate) on Sep 26, 2006 at 02:51 UTC | |
by ikegami (Patriarch) on Sep 26, 2006 at 15:08 UTC | |
|
Re: are TypeGlob assingments to a lexical variables possible?
by shmem (Chancellor) on Sep 25, 2006 at 14:45 UTC |