Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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 # Piece_m.pl -- 'Moose' version of Piece.pm 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…)

--hsm

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

In reply to Re^2: Hiding trigger code from meta by hsmyers
in thread Hiding trigger code from meta by hsmyers

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (8)
    As of 2014-07-25 03:45 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (167 votes), past polls