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

Re^4: Mocking a method defined in a Moo Role

by PopeFelix (Beadle)
on Jun 24, 2016 at 18:25 UTC ( [id://1166530]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Mocking a method defined in a Moo Role
in thread Mocking a method defined in a Moo Role

Thanks to mst on #moose:

MyClass.pm

package MyClass; use Moo; use FindBin qw($Bin); use lib $Bin; with 'MyRole'; around foo => sub { my ($orig, $self) = @_; return 'Wrapped ' . $self->$orig; };

rolesketch.pl

use 5.016; use FindBin qw($Bin); use lib qq($Bin); require MyRole; our $orig = MyRole->can('foo'); no warnings 'redefine'; *MyRole::foo = sub { goto &$orig }; { local $orig = sub {'baz'}; require MyClass; my $obj = MyClass->new; my $res = $obj->foo; if ( $res =~ /baz$/ ) { say qq{OK, got $res}; } else { say qq{Not OK, got $res}; } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1166530]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-03-19 08:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found