[sf-perl] the mouse with antlers

Bill Moseley moseley at hank.org
Wed Apr 4 18:01:23 PDT 2012


On Thu, Apr 5, 2012 at 8:38 AM, Joseph Brenner <doom at kzsu.stanford.edu>wrote:

> I've been messing about with Mouse.pm of late (a lightweight Moose
> substitute whose star may already be fading), and I've got a basic
> question/complaint.
>
> I'm used to doing href based objects with an init routine that's
> called by new, where I can do any initialization I might need to do.
> In Moose land I find myself doing things like this:
>

There's BUILD (and BUILDARGS) if you want to hook into construction.

http://search.cpan.org/~doy/Moose-2.0403/lib/Moose/Manual/Construction.pod


>
> use Moose
> has name        =>
>     (is => 'rw',  isa => 'Str', required => 1);
> has subject     =>
>     (is => 'rw',  isa => 'Str', default => sub{ 'Report about .
> $_[0]->name }, lazy => 1);
> has id              =>
>     (is => 'rw',  isa => 'Str', default => sub{  lookup( $_[0]->name
> ) },          lazy => 1);
>
> Where the "lazy" setting delays the use of the initialization code to
> the time when the field is first accessed, and that way I can be
> certain that the name field has settled down.
>

I'm wondering if you don't really want to be using a builder instead of a
default.   I tend to make heavy use of builders (mostly using lazy_build =>
1) as that makes the code, well, build itself.



> I find myself worrying about what I would do if I needed more than two
> stages, instantiation and lazy access.  Like what if I wanted to put
> the id in the subject?
>

has id => ( lazy_build => 1, ... );
sub _build_id { return shift->name }

has subject => ( lazy_build => 1, ... )l
sub _build_subject { return 'Report about '  . shift->id }


-- 
Bill Moseley
moseley at hank.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/sanfrancisco-pm/attachments/20120405/90d52978/attachment-0001.html>


More information about the SanFrancisco-pm mailing list