Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: On timely destruction?

by TheDamian (Priest)
on Aug 28, 2002 at 07:47 UTC ( #193380=note: print w/replies, xml ) Need Help??

in reply to On timely destruction?

A typical problem is how to wrap the accessor methods of a class, so that some check is performed once the returned value is dispensed with. For example, suppose you have to maintain some code that provides direct access to the "name" field stored within a CachedFile object. You might have:

package CachedFile; sub new { my ($class, $name) = @_; bless { name => $name , contents => "" }, $class; } sub name { my ($self) = @_; return \$self->{name}; }

But because the class gives out direct access to the name, you don't have control of it. If you need to ensure that the name doesn't exceed 12 characters in length, you have no way to do so; any changes to the name field will occur after CachedFile::name has finished:

${$cachedfile->name} = "a_long_file_name";

One solution is not to return a reference to the name field at all. Instead, you return a reference to an imposter, which then forwards all requests to the real name field.

When the full expression in which this imposter was created is finished, the last reference to the imposter will disappear and its destructor will be called and can then check for foul play.

The class that implements the imposter or "proxy" looks like this:

package Proxy; sub for { tie my($proxy), $_[0], @_[1..3]; return \$proxy; } sub TIESCALAR { my ($class, $original, $postcheck, $message) = @_; bless { original => $original, postcheck => $postcheck, message => $message, }, $class; } sub FETCH { my ($self) = @_; return ${$self->{original}}; } sub STORE { my ($self, $newval) = @_; ${$self->{original}} = $newval; } sub DESTROY { my ($self) = @_; croak $self->{message} unless $self->{postcheck}->($self->{original}); }

The CachedFile class would then set up its name accessor like so:

package CachedFile; sub new { my ($class, $name) = @_; bless { name => $name , contents => "" }, $class; } sub name { my ($self) = @_; return Proxy->for(\$self->{name}, sub{ length(${$_[0]}) <= 12 }, "File name too long!" ); }

Now any attempt to assign an extravagant name causes an exception to be thrown:

my $file = CachedFile->new("orig_name"); ${$file->name} = "shrt_fl_nm"; # okay ${$file->name} = "a_long_file_name"; # KABOOM!

There are many such idioms that rely on proxy objects being destroyed at the end of the statement in which they're created (rather than at the end of the surrounding scope). So setting an end-of-scope action doesn't help these cases, since we want the effects to have been applied much earlier than that: before the next statement, in fact.

Replies are listed 'Best First'.
Re: Re: On timely destruction?
by Elian (Parson) on Aug 28, 2002 at 08:33 UTC
    Well, that's one way to go about it. For something like that, where the data element has some sort of restrictions on it, you're much better off using typed data for the element you need to have escape, and let the class the data is blessed into pitch a fit in the event an assignment to it violates the constraits on that data. Since typed data can overload assignment, it's an easy way to have action-at-a-distance validation of data going into variables.

    That eliminates the need for proxy objects and suchlike hackery, and provides a cleaner interface. I expect someone (like, say... you? :) will come up with a constraints module such that you can say:

    my $foo has constraint({length $^a < 12});

    To allow tagging on contstraint conditions. (Assuming, in this case, that constraint takes a list of closures which all must return true to allow the assignment) Modulo proper perl 6 syntax, of course.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://193380]
and the fire pops...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2017-03-30 03:05 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (353 votes). Check out past polls.