develooper Front page | perl.perl5.porters | Postings from April 2011

Re: Revising Perl's OO docs - a new OO tutorial

Thread Previous | Thread Next
Paul LeoNerd Evans
April 15, 2011 04:55
Re: Revising Perl's OO docs - a new OO tutorial
Message ID:
On Thu, Apr 14, 2011 at 05:24:18PM +0200, Aristotle Pagaltzis wrote:
> >  use Sentinel;
> >
> >  sub attr :lvalue
> >  {
> >     my $self = shift;
> >     sentinel fetch => sub { return $self->get_foo },
> >              store => sub { $self->set_foo( shift ) };
> >  }
> That would be awesome.
> Something like this is already doable with `tie` of course. It’s
> just terribly slow. I presume Variable::Magic would be faster?
> (It doesn’t have to do method dispatch after all.)

V::M or native XS would indeed be much faster. Actually I'm thinking
native XS; see below...

On Thu, Apr 14, 2011 at 09:53:16PM +0200, Leon Timmermans wrote:
> I've seen cases where Variable::Magic was an order of magnitude faster
> than ties, though it also depends on what you're doing. An efficient
> Sentinel may want to avoid the fetching when possible.

That's true. In fact, in the normal rvalue case, no need to create the
magic SV at all:

  my $foo = $self->foo;

The 'sentinel' function can detect it's being called not in modification
context, so doesn't need to build the sentinel SV with magic at all. It
can just call the fetch code directly, and yield that value.

The only case where interesting fetch-and-store semantics would take
place is on proper mutation behaviours:


  substr( $self->title, 0, 50, "Some new title here" );

However; my original intent was simply to point out this would be easy
to create.. not -necessarily- that I'd actually got and make it. If it's
deemed useful then I might consider writing it, but I didn't intend to
get stuck into that, necessarily...

Paul "LeoNerd" Evans
ICQ# 4135350       |  Registered Linux# 179460

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About