MooseX::WhatTheTrig - what attribute triggered me?
use v5.14; package Goose { use Moose; use MooseX::WhatTheTrig; has foo => ( traits => [ WhatTheTrig ], is => 'rw', trigger => sub { my $self = shift; my $attr = Moose::Util::find_meta($self)->triggered_attribute; say "Triggered $attr"; }, ); } my $obj = Goose->new(foo => 42); # says "Triggered foo" $obj->foo(999); # says "Triggered foo"
Moose trigger subs get passed two (sometimes three) parameters:
The object itself.
The new attribute value.
The old attribute value (if any).
The sub doesn't get told which attribute triggered it. This may present a problem if you wish to have the same coderef triggered from several different attributes.
This module adds a $meta->triggered_attribute method to your class' metaobject, which allows you to check which attribute has been triggered.
$meta->triggered_attribute
Yes, it works if you trigger one attribute from another attribute.
Yes, it works in roles.
Yes, it works with inheritance.
Please report any bugs to http://rt.cpan.org/Dist/Display.html?Queue=MooseX-WhatTheTrig.
http://stackoverflow.com/questions/22306330/moose-trigger-caller.
Toby Inkster <tobyink@cpan.org>.
This software is copyright (c) 2014 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
To install MooseX::WhatTheTrig, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MooseX::WhatTheTrig
CPAN shell
perl -MCPAN -e shell install MooseX::WhatTheTrig
For more information on module installation, please visit the detailed CPAN module installation guide.