FAQ
I can do this:

my $hashref = \%hash;

But as near as I can tell, there is no way to do the reverse
operation: making a "my %hash" that is an alias for a hash reference.

It's possible with a package variable:

#/usr/bin/perl
our %hash;
my $hashref = { a => 5 , b => 8 };
*hash = $hashref;
print $hash{a};

that prints "5". But there's no way to do this with a lexical (my)
variable.

is this right?

--
Aaron Priven, aaron@priven,com, http://www.priven.com/aaron

Search Discussions

  • Jeremy Kister at Oct 29, 2007 at 11:33 pm

    On 10/29/2007 7:23 PM, Aaron Priven wrote:
    my $hashref = \%hash;

    But as near as I can tell, there is no way to do the reverse
    operation: making a "my %hash" that is an alias for a hash reference.
    my %newhash = %{$hashref};
  • Tom Phoenix at Oct 30, 2007 at 12:22 am

    On 10/29/07, Jeremy Kister wrote:
    On 10/29/2007 7:23 PM, Aaron Priven wrote:
    my $hashref = \%hash;

    But as near as I can tell, there is no way to do the reverse
    operation: making a "my %hash" that is an alias for a hash reference.
    my %newhash = %{$hashref};
    You have made a copy of the hash. I do not believe that that is what
    the original poster wished, which I think would be a new hash that the
    old reference points to. I don't think that what the original poster
    wants is even possible, but I can't see any reason to need it, either.

    Cheers!

    --Tom Phoenix
    Stonehenge Perl Training
  • Paul Johnson at Oct 30, 2007 at 12:34 am

    On Mon, Oct 29, 2007 at 05:21:52PM -0700, Tom Phoenix wrote:
    On 10/29/07, Jeremy Kister wrote:
    On 10/29/2007 7:23 PM, Aaron Priven wrote:
    my $hashref = \%hash;

    But as near as I can tell, there is no way to do the reverse
    operation: making a "my %hash" that is an alias for a hash reference.
    my %newhash = %{$hashref};
    You have made a copy of the hash. I do not believe that that is what
    the original poster wished, which I think would be a new hash that the
    old reference points to. I don't think that what the original poster
    wants is even possible, but I can't see any reason to need it, either.
    Perhaps something like Data::Alias, for example, might do the trick.

    --
    Paul Johnson - paul@pjcj.net
    http://www.pjcj.net
  • Aaron Priven at Oct 30, 2007 at 5:17 am
    Playing around with this, indeed, proves it can be done:

    #!/usr/bin/perl
    use strict;
    use warnings;
    use Data::Alias;
    my $hashref = { a => 5, b => 8};
    alias my %hash = %{$hashref};
    print $hash{a} , "\n";

    prints 5. (And \%hash == $hashref evaluates true.)

    I knew that Data::Alias existed -- it's mentioned in Perl Best
    Practices -- but at least to me, the documentation seemed to imply
    that it couldn't do this, or that it would act like "many scalar
    assignments in parallel".

    Thanks!
    On Oct 29, 2007, at 5:33 PM, Paul Johnson wrote:
    Perhaps something like Data::Alias, for example, might do the trick.
    --
    Aaron Priven, aaron@priven.com, http://www.priven.com/aaron
  • Jeremy Kister at Oct 30, 2007 at 2:22 am

    On 10/29/2007 8:21 PM, Tom Phoenix wrote:
    my %newhash = %{$hashref};
    You have made a copy of the hash. I do not believe that that is what
    the original poster wished, which I think would be a new hash that the
    old reference points to. I don't think that what the original poster
    wants is even possible, but I can't see any reason to need it, either.
    Oops. I knew I was making a new hash (hence the variable name). I read
    his message (and subject) to imply that all he wanted was dereferencing.
    My fault.
  • Aaron Priven at Oct 30, 2007 at 5:21 am

    On Oct 29, 2007, at 5:21 PM, Tom Phoenix wrote:
    I don't think that what the original poster
    wants is even possible, but I can't see any reason to need it, either.
    It's probably not necessary per se, as you can always do anything
    using reference syntax, but it's awkward and requires lots of extra
    dereferencing by the interpreter.

    If %hash has a few thousand entries,

    routine (%hash);

    sub routine {
    my %myhash = @_;
    ...
    }

    is wasteful and timeconsuming, and I don't want to have to type lots
    of extra arrows just because I passed the hash to a subroutine. And
    now I don't have to. Yay.

    --
    Aaron Priven, aaron@priven.com, http://www.priven.com/aaron
  • Tom Phoenix at Oct 30, 2007 at 12:26 am

    On 10/29/07, Aaron Priven wrote:

    I can do this:

    my $hashref = \%hash;

    But as near as I can tell, there is no way to do the reverse
    operation: making a "my %hash" that is an alias for a hash reference.
    You mean, given a reference to a hash, create a named lexical hash
    that that reference refers to? No, that's not possible. Why would you
    even want to do that? Is it just a matter of convenience, so you can
    avoid dereferencing?

    As you found, you can manipulate the symbol table so that a name
    refers to a hash from a reference, but lexical variables aren't in the
    symbol table.

    Cheers!

    --Tom Phoenix
    Stonehenge Perl Training
  • John W . Krahn at Oct 30, 2007 at 12:51 am

    On Monday 29 October 2007 15:23, Aaron Priven wrote:
    I can do this:

    my $hashref = \%hash;

    But as near as I can tell, there is no way to do the reverse
    operation: making a "my %hash" that is an alias for a hash reference.
    Correct. You can alias a package variable but not a lexical variable.

    It's possible with a package variable:

    #/usr/bin/perl
    our %hash;
    my $hashref = { a => 5 , b => 8 };
    *hash = $hashref;
    You are not assigning to %hash you are assigning to the typeglob *hash.
    Because $hashref contains a reference to a hash only the *hash{ HASH }
    entry in the symbol table is affected.

    print $hash{a};

    that prints "5". But there's no way to do this with a lexical (my)
    variable.

    is this right?
    Correct. Lexical variables are not part of the symbol table.



    John
    --
    use Perl;
    program
    fulfillment

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedOct 29, '07 at 11:23p
activeOct 30, '07 at 5:21a
posts9
users6
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase