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";
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|