Re: dereferencing



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

.



Relevant Pages

  • Re: What dont I understand about hash assigments?
    ... numeric codes with the codes starting with "H..." ... What don't I understand about assigning values to a hash? ... But then when you get to the print statement, ...
    (perl.beginners)
  • Re: perl inbuilt function each may be having a bug
    ... This code is not working and assigning values to '', ... Use of uninitialized value in concatenation or string at each- ... Why are you using a two-level hash to demonstrate the behavior? ... that will start iterating again. ...
    (comp.lang.perl.misc)
  • Re: %a = {}; What is this?
    ... But you're assigning that to the hash ... %a is a hash, not a reference. ... Some versions of Perl will give no ...
    (perl.beginners)
  • Re: Question about arrays of hashes
    ... light for me on weirdness I'm seeing when trying to access a particular hash from an array-of-hashes. ... I had a lot of difficulty assigning a particular element to its own variable, but after some experimentation I found that this works: ... I have to first create the variable in scalar context, then do a funky dereferencing thing while changing the context to hash. ...
    (comp.lang.perl.misc)
  • Re: CryptSetHashParam
    ... know how big your hash value is, and which OID to place in the signature ... Please do not send email directly to this alias. ... > I was trying to sign the data of a hash that i got outside the cryptoAPI. ...
    (microsoft.public.platformsdk.security)