Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Hiding trigger code from meta

by hsmyers (Canon)
on Jan 19, 2011 at 11:14 UTC ( #883090=perlquestion: print w/replies, xml ) Need Help??

hsmyers has asked for the wisdom of the Perl Monks concerning the following question:

In a Moose object is there a way to hide the code used for a trigger? It has occurred to me that I could use an anonymous declaration for it, but I was just wondering if there was something already in use.


"Never try to teach a pig to wastes your time and it annoys the pig."

Replies are listed 'Best First'.
Re: Hiding trigger code from meta
by stvn (Monsignor) on Jan 19, 2011 at 19:11 UTC

    The standard way is to use an anon-sub, so that is what I would recommend. The only real reason for not using an anon-sub is because you want to be able to use the trigger code in another context, in which case you would want it to show up as a method via meta. Remember that the trigger code is passed $self as the first argument so it works just like a method anyway.

    Perhaps you might want to elaborate on the reasoning behind wanting this behavior, it might allow us to give you a better solution.

      Given that trigger is used (at least by me) to verify/limit the value of what we used to call a class variable (do we call them attributes now?) I mostly wanted to avoid the case where the silly but otherwise innocent user of my code calls a function thinking it might be just the thing only to have it; a. not be 'the thing' and b. screw things up. Here is the small class where this came up:
      #!/usr/bin/perl # -- 'Moose' version of use strict; use feature ":5.10"; use Data::Dumper::Simple; our $VERSION = '0.02'; package Piece; use Moose; use Moose::Util::TypeConstraints; my %pieces; enum 'Side' => qw(white black); has 'side' => ( is => 'ro', isa => 'Side', required => 1 ); enum 'Name' => qw(K Q R B N P); has 'name' => ( is => 'ro', isa => 'Name', required => 1 ); enum 'ID' => qw ( WQR WQN WQB WQ WK WKB WKN WKR WQRP WQNP WQBP WQP WKP WKBP WKNP WKRP BQR BQN BQB BQ BK BKB BKN BKR BQRP BQNP BQBP BQP BKP BKBP BKNP BKRP ); has 'id' => ( is => 'ro', isa => 'ID', required => 1, trigger => \&che +ck_id ); sub check_id { my $self = shift; if ( exists( $pieces{ $self->{id} } ) ) { confess "", $self->{id}, " already declared"; } else { $pieces{ $self->{id} }++; } } package Main; use Test::More 'no_plan'; use Test::Fatal; my $wk = Piece->new( side => 'white', name => 'K', id => 'WK' ); isa_ok( $wk, 'Piece' ); isa_ok( $wk, 'Moose::Object' ); is( $wk->side, 'white', '... got the right side for wk' ); is( $wk->name, 'K', '... got the right name for wk' ); is( $wk->id, 'WK', '... got the right id for wk' ); my @methods = Piece->meta->get_method_list(); print Dumper(@methods);
      As you can see an invocation of Piece->check_id() would not be a good thing— although having just said that I suppose I could examine the passed parameters and be able to tell if the call was correct or not. If not complain else do what it was designed to do. At the moment this is a toy so that I can begin to understand Moose with an eye towards converting my CPAN stuff accordingly (21st century and all that…)


      "Never try to teach a pig to wastes your time and it annoys the pig."

        Honestly, I see no reason why you would be worried about someone calling check_id, it will never blow up on them since it will always check the id which is already set. If you want to discourage people from calling it, name it _check_id.

        Also, you shouldn't access the hash directly, you should call $self->id not $self->{id}

Re: Hiding trigger code from meta
by jethro (Monsignor) on Jan 19, 2011 at 17:15 UTC

    Nobody did bite yet, so until someone with more knowledge or our ultimate authority for Moose stvn gets around to answer, I give you at least my best answer

    I just read (in a book about Catalyst which uses Moose heavily) that there are two ways for private subroutines in Moose:

    1) Use '_' before a subroutine name. This is only a convention, but in most cases it is evident that you should not use such a function outside the class

    2) Use an anonymous subroutine (as you already found out). Don't think of it as cheating or second-rate subroutining ;-), it is a fine non-namespace-cluttering way to put a private subroutine into a class, the only drawback is that code reuse needs a bit more effort.

    Also there is namespace::clean which can be used to forget those subroutine names again as soon as you don't need them anymore. I don't know if this would help in your case (it depends from whom you want to hide the trigger), but the module is worth a look

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://883090]
Approved by rovf
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2023-12-06 16:33 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (30 votes). Check out past polls.