Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Syntactic Confectionery Delight
 
PerlMonks  

Re: Alias $hashref referent to a hash

by kvale (Monsignor)
on Oct 22, 2004 at 14:03 UTC ( [id://401600]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Alias $hashref referent to a hash

Lexical aliases seem to work fine:
my %hash = (1 => 2, 3 => 4); my $ref = \%hash; my $alias = $ref; print "hash = ", %hash, "\n"; print "ref hash = ", %$ref, "\n"; print "alias hash = ", %$alias, "\n";
prints
hash = 1234 ref hash = 1234 alias hash = 1234
What is the code you are having trouble with?

-Mark

Replies are listed 'Best First'.
Re^2: Alias $hashref referent to a hash
by hlen (Beadle) on Oct 22, 2004 at 14:11 UTC
    Nah, that's still a reference. I want a %hash to be an alias to the referent, not a $scalar to keep the same reference.

    Thanks

      So you want to be able to do something like this:

      my $ref = {thing => 'value', otherthing => 'other'}; %alias_to_ref = ;# insert magical code here # to create a hash which will # still modify $ref $alias_to_ref{thing} = 'newvalue'; print $ref->{thing};

      And have that output 'newvalue'? If that is what you want, you probably could do it with a tied hash. See Below:

      #!/usr/local/bin/perl -w use strict; my($ash) = {thing => 'other', and => 'antoher'}; my(%hash); tie %hash, 'MyHash', $ash; $hash{thing} = 'new'; print $ash->{thing}."\n"; package MyHash; require Tie::Hash; @MyHash::ISA = qw(Tie::StdHash); sub STORE { my $me = shift; my $key = shift; $me->{$key} = shift; } sub TIEHASH { my $package = shift; my $ref = shift; return bless($ref, $package); }

      This outputs 'new'.

      May the Force be with you

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://401600]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.