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

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

Thread Previous | Thread Next
From:
Aristotle Pagaltzis
Date:
April 14, 2011 08:24
Subject:
Re: Revising Perl's OO docs - a new OO tutorial
Message ID:
20110414152417.GF3627@klangraum.plasmasturm.org
* Paul LeoNerd Evans <leonerd@leonerd.org.uk> [2011-04-13 16:00]:
> On Mon, Mar 07, 2011 at 08:35:41AM +0100, Johan Vromans wrote:
> > > If we do add object attributes to Perl, it would be very
> > > nice if we could use ‘$obj->attr = $foo’ syntax, instead of
> > > $obj->attr($foo), which is currently widely used mainly
> > > because the former is so hard to do (that can change, of
> > > course).
> >
> > This would elegantly solve the semi-religious problem of using
> >
> >     $obj->attr("foo") and $obj->attr
> > vs
> >     $obj->set_attr("foo") and $obj->get_attr
> > vs
> >     $obj->SetAttr("foo") and $obj->GetAttr
> >
> > I think this is where :lvalue was invented for.
>
> I've been repeatedly pondering on just such an idea.
>
> Posit the existance of Sentinel.pm:
>
>  use Sentinel;
>
>  sub attr :lvalue
>  {
>     my $self = shift;
>     sentinel fetch => sub { return $self->get_foo },
>              store => sub { $self->set_foo( shift ) };
>  }
>
> I further submit that Sentinel.pm is writable in about 10 lines
> of pureperl using Variable::Magic, or similar plus its own tiny
> .xs file. Neither approach, however, is pureperl in its entire
> deptree.

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.)

-- 
*AUTOLOAD=*_;sub _{s/::([^:]*)$/print$1,(",$\/"," ")[defined wantarray]/e;chop;$_}
&Just->another->Perl->hack;
#Aristotle Pagaltzis // <http://plasmasturm.org/>

Thread Previous | Thread Next


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