develooper Front page | perl.perl6.language | Postings from June 2005

Re: Magic mutators and my $var is Proxy( ... );

Thread Previous
From:
Sam Vilain
Date:
June 26, 2005 15:43
Subject:
Re: Magic mutators and my $var is Proxy( ... );
Message ID:
42BF2F80.7080009@vilain.net
Sam Vilain wrote:
> To me it is a trivial case that you want to provide a fake attribute
> which for all intents and purposes behaves exactly like there was a real
> attribute there, backing against another attribute.
> 
> A Date object is a classic example of this; you want to provide 0-based
> and 1-based attributes, which you want to present as equivalent to each
> other.

FWIW, I think this question also applies to fetching members from a hash
collection.

For example, if you have a function;

   sub foo($foo) { $foo || "bar" }

And you call it, passing it a hash slot;

   foo(%hash<key>);

Then it will be auto-vivified, as in Perl 5; because a reference to the
slot had to be taken.  Actually there is no reference taken; it's a
read-only binding of the hash slot.  But either way, it is a read-only
point in the signature that is affecting the input argument, which is
arguably (right^wrong).

By returning a Proxy object 'tied' to the slot, then this autovivification
could be circumvented and the straightforward implementation - passing
bound variables as transparent references, continues to be acceptable.
Albeit requiring that the Hash::postcircumfix:<{ }> method is a Proxy
method, too.

Sam.

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About