Just to be sure, I tried
and so... wow! it seems that we really have a LVALUE datatype. Now, that's cool; OTOH we have lvalue-able subs, but what about them?$ perl -le 'print ref \substr "foo", 1' LVALUE
Huh, no, it's just a plain old SCALAR. Which raises this meditation: lvalue subs can be cool, but can I imitate substr's action at distance behaviour to do something potentially more interesting? Well, it turns out that I can:$ perl -le 'my $u; sub u :lvalue {$u} print ref \u' SCALAR
#!/usr/bin/perl -l use strict; use warnings; package Append; sub TIESCALAR { bless \$_[1], $_[0] } sub FETCH { ${${ $_[0] }} } sub STORE { ${${ $_[0] }} .= $_[1] } package main; sub append :lvalue { tie my $v, 'Append', \$_[0]; $v; } my $u='Foo'; (append $u)='Bar'; print $u; __END__
Incidentally, if I substituteWhatever, this is at best tricky. And this is an oversimpliflied example. It could get too complex in a more serious attempt.with(append $u)='Bar'; print $u;it still works, whereas I would have expected it to raise an error about an attempt to modify a read-only value. Why doesn't it?print +(append 'Foo')='Bar';
So getting back to the lvref topic hinted to at the beginning of this post, the matter is: the above could be doable in an easier way if
What about this idea? (Or some variation thereof!)sub append :lvalue { local *LVALUE::STORE = sub { my $self=shift; $$self .= $_[0]; } $_[0]; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: lvalues and action at distance
by BrowserUk (Patriarch) on Oct 11, 2005 at 18:59 UTC | |
by blazar (Canon) on Oct 12, 2005 at 07:28 UTC | |
|
Re: lvalues and action at distance
by japhy (Canon) on Oct 11, 2005 at 14:40 UTC | |
by blazar (Canon) on Oct 11, 2005 at 15:03 UTC | |
|
Re: lvalues and action at distance
by ysth (Canon) on Oct 11, 2005 at 18:44 UTC | |
|
Re: lvalues and action at distance
by perrin (Chancellor) on Oct 11, 2005 at 17:57 UTC | |
by blazar (Canon) on Oct 12, 2005 at 13:03 UTC | |
|
Re: lvalues and action at distance
by sauoq (Abbot) on Oct 12, 2005 at 02:59 UTC |