Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Moose: apply Role at Runtime (build vs. default)

by tobyink (Abbot)
on Apr 02, 2013 at 16:13 UTC ( #1026729=note: print w/replies, xml ) Need Help??


in reply to Moose: apply Role at Runtime (build vs. default) [solved]

Works for me!

use v5.14; use strict; use warnings; package MyObject { use Moose; } package MyRole { use Moose::Role; has myattr => ( is => 'ro', lazy => 1, builder => '_build_myattr', ); sub _build_myattr { "value"; } } my $obj = "MyObject"->new; Moose::Util::apply_all_roles($obj, "MyRole"); say $obj->myattr;

Also, whenever you call a builder method from a coderef, make sure you call it as a method so that subclassing works.

# Not this... default => \&_build_myattr, # This! default => sub { shift->_build_myattr(@_) },
package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Replies are listed 'Best First'.
Re^2: Moose: apply Role at Runtime (build vs. default)
by Brutha (Friar) on Apr 04, 2013 at 14:16 UTC

    First, thank you for your answer. Guess what, it works for me as well :(

    The reason for my late answer is, that my problem is a bit more complex. I have a base class and create on the fly a Role and then a Class from that Role and the baseclass. Later I apply that role with the default/builder problem. Putting that together in my sample code works as well with builders.

    Now I try to figure out where the difference is between my real code and the sample, as the real code still does give that error. (Not) Using MooseX::Declare does not make a difference.

    And it came to pass that in time the Great God Om spake unto Brutha, the Chosen One: "Psst!"
    (Terry Pratchett, Small Gods)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1026729]
help
Chatterbox?
[tye]: that is what getlogin() certainly *used* to do. I don't believe that is what it certainly should do.
[davido]: /var/run/utmp is 664 i think.
[tye]: Note that my "man getlogin" says that it uses stdin when it should use /dev/tty (calling a glibc bug). But that does not appear to be the case when I test it. But maybe Perl's getlogin() is not using glibc's getlogin().
[oiskuu]: well, run a strace and see what the getlogin does for you.... As I said. SELinux probably has those security labels. But not regular linux.
[tye]: for example, read https://unix. stackexchange.com/ questions/146138/ loginuid-should-be -allowed-to-change -or-not-mutable-or -not
[tye]: I'm not using SELinux and it certainly appears to disagree with you. shrug
[tye]: Since you brought up /proc, oiskuu, I didn't see you respond to my suggestion of 'loginuid'. Does your /proc not have such?

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2017-06-23 19:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (554 votes). Check out past polls.