Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re^3: Changing a variable which is a value of a hash

by choroba (Cardinal)
on Apr 16, 2015 at 19:15 UTC ( [id://1123656]=note: print w/replies, xml ) Need Help??

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

I tried to implement my solution with tying, too. The difference to your solution is there's no need to indicate what value you want: Perl just does what you mean, as usually:
#!/usr/bin/perl use warnings; use strict; { package MagicScalar; use Tie::Scalar; use parent qw( -norequire Tie::StdScalar ); use overload nomethod => \&stringify; sub TIESCALAR { my $class = shift; bless [], $class } sub STORE { my ($self, $value) = @_; $self->[0] = $value; } sub FETCH { my $self = shift; return $self } sub stringify { my $self = shift; return $self->[0] } } { package MagicHash; use Tie::Hash; use parent qw( -norequire Tie::StdHash ); sub STORE { my ($self, $key, $value) = @_; my $previous = $self->{$key}; if (ref $previous and $previous->isa('MagicScalar')) { $previous->STORE($value); } else { $self->{$key} = $value } } } tie my $test, 'MagicScalar'; $test = 'test'; tie my %hash, 'MagicHash'; %hash = (t => $test); print "Before $hash{t} $test\n"; my $change = 'changed'; $hash{t} = $change; print "After $hash{t} $test\n";
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1123656]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-07-22 10:57 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.