Re: RFC 132 (v3) Subroutines should be able to return an lvalue

[email protected] (Damian Conway) Fri, 1 Sep 2000 12:13:59 +1100 (EST)
Newsgroups perl.perl6.language.subs
Message-ID <[email protected]>
   > I've been thinking about this for a couple days. The only problem I see
   > is that this doesn't allow me to do this:
   > 
   >    $oldpath = $tree->path('L','R') = 'R';
   >    @document = ($title, $junk, $r->xml_extract) = <STDIN>;
   > 
   > I would still have to use some yeechy combination with tie under your
   > RFC.

You can do this easily, even under Perl 5, with the Class::Proxy module:

	package Tree;
	use Class::Proxy;

	sub path {
		my ($self, @args) = @_;

		# walk down path to find referent:

		my $ref = $self;
		$ref = $ref->{$_} foreach (@_);

		# return a proxy for it,
		# with personalized assignment semantics:

		return proxy $ref,
			STORE => { die unless $_[1] =~ /[LR]/; $_[0] = $_[1] };
	}

It isn't yet on the CPAN, but if people want it, I'll bump its priority
and get it out next week.

Damian