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

    There's not a native Perl way to do that, but there is japhy's Lexical::Alias. Watch this!

    use strict; use warnings; use Lexical::Alias; my $src = { one => 1, two => 2 }; my %dest; alias %{$src}, %dest; print keys %dest, "\n"; $dest{three} = 3; print $src->{three}, "\n";

    Dave

      Thanks. By the way do you know if that carries a huge overhead?
Re: are TypeGlob assingments to a lexical variables possible?
by diotalevi (Canon) on Sep 25, 2006 at 14:12 UTC

    I am under the impression that the current state of the art is Data::Alias.

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re: are TypeGlob assingments to a lexical variables possible?
by ikegami (Patriarch) on Sep 25, 2006 at 15:45 UTC
    It's safer to do local *H than local %H, and particularly convenient here. Also, there's no reason to use our %H globally.
    use strict; sub foo { my $Href = shift; our %H; local *H = $Href; # define the alias print $H{1}; }

    The downside to using local *H is that you can't access $H, @H, &H, etc in foo.

      thanks. But I'm not sure why it is "safer" to localize *H than specifically %H. I guess what you are worried about that the passed in reference might not be a hash. Right?
        No, it's because it prevents bad things from happening if the parent %H is a tied variable or an alias to a tied variable.
Re: are TypeGlob assingments to a lexical variables possible?
by shmem (Chancellor) on Sep 25, 2006 at 14:45 UTC
    Another way to implement lexical typeglobs is via Hacking perl...

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}