Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re: On timely destruction? by TheDamian
in thread On timely destruction? by Elian

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (9)
As of 2024-04-18 12:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found