in reply to Re: Alias $hashref referent to a hash
in thread Alias $hashref referent to a hash

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

  • Comment on Re^2: Alias $hashref referent to a hash

Replies are listed 'Best First'.
Re^3: Alias $hashref referent to a hash
by JediWizard (Deacon) on Oct 22, 2004 at 18:33 UTC

    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