From the OP: "#I just want to change $test to 'changed' not $hash{t} to $change"

From that i believe that OP wants the output like so:

_END__ Output: Before test test ... After test changed ...

Which is boggling to me ...

UPDATE: ... so i implemented it using tied hashes:

#!/usr/bin/env perl package MagicHash; require Tie::Hash; our @ISA = qw(Tie::Hash); sub TIEHASH { bless {}, shift } sub STORE { my ($self, $key, $newval) = @_; $self->{$key} = {key=>$key, value=>[$newval,$$newval]}; } sub FIRSTKEY { my ($self) = @_; keys %{$self}; my $first_key = each %{$self}; return undef unless defined $first_key; return $self->{$first_key}->{key}; } sub FETCH { my ($self, $key) = @_; my ($is_ref) = $key =~ s/^\\//; return $is_ref ? $self->{$key}->{value}->[0] : $self->{$key}->{value}->[1] ; } sub NEXTKEY { my ($self, $nextkey) = @_; my $next_key = each %{$self}; return undef unless defined $next_key; return $self->{$next_key}->{key}; } package main; use strict; use warnings; use Data::Dumper; { my $test = 'test'; my $changed = 'changed'; my %hash = ( t => \$test ); print "Before: \$test = $test\n", Dumper \%hash; ${ $hash{t} } = 'changed'; print " After: \$test = $test\n", Dumper \%hash; } { my $test = 'test'; my $changed = 'changed'; tie my %hash, 'MagicHash'; %hash = ( t => \$test ); print "Tied Before: \$test = $test\n", Dumper \%hash; ${ $hash{'\t'} } = 'changed'; print "Tied After1: \$test = $test\n", Dumper \%hash; ${ $hash{'\t'} } = 'again'; print "Tied After2: \$test = $test\n", Dumper \%hash; $hash{'t'} = \'one more'; print "Tied After3: \$test = $test\n", Dumper \%hash; }
The trick is to store the value of the var and the reference to it in an anonymous array reference and then use some identifier (i chose the backslash) to distinguish which one is needed at the time. Very silly stuff. Sorry 'bout that. :)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

In reply to Re^2: Changing a variable which is a value of a hash by jeffa
in thread Changing a variable which is a value of a hash by benedicth

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.