Hi,

I am new to Moose and came across a scenario that seemed confusing to me. I am wondering if this use case was designed to be this way or if it needs more consideration. Here is my code:

ScoreBoard.pm -------------------- package ScoreBoard; use Moose; use MooseX::Privacy; has 'started' => ( traits => [qw/Bool Protected/], is => 'ro', isa => 'Bool', predicate => 'hasGameStarted', handles => { startGame => 'set', }, ); 1; ScoreBoard-test2.pl -------------------------- #!/opt/local/bin/perl -w use strict; use ScoreBoard; my $sb = ScoreBoard->new; print "Game has not started yet\n" if !$sb->hasGameStarted;

Here is the error message I get when I execute the test script:

$ ./ScoreBoard-test2.pl Attribute started is protected at /opt/local/lib/perl5/site_perl/5.12. +3/MooseX/Privacy/Meta/Attribute/Protected.pm line 13. [snip ...]

As you can see from my module, I created a "Protected" attribute and defined a predicate method 'hasGameStarted' and a delegated method 'startGame'. The program failed due to the interaction between the "Protected" status of the attribute and the predicate method. So I changed my module to the following:

has 'started' => ( traits => [qw/Bool Protected/], is => 'ro', isa => 'Bool', #predicate => 'hasGameStarted', handles => { startGame => 'set', }, ); sub hasGameStarted { my $self = shift; return defined $self->started; }

This time, the program runs successfully and I get the expected output:

$ ./ScoreBoard-test2.pl Game has not started yet

Now, to my point, the part that confuses me. I originally defined the two methods directly under the "Protected" attribute. The predicate method failed, whereas the delegated method succeeded. It seems to me that either they should both fail or they should both succeed. I would prefer that they both succeed personally, but I'm sure I have a very narrow view at this point. My rationale is that the "Protected" status of the attribute is to prevent the caller from accessing the attribute, in this case 'started'. But the caller is calling the '$sb->hasGameStarted' method and at no time ever attempts to access '$sb->started' (that happens behind the scenes in the module, but the caller wouldn't know that directly). It seems logical to me to allow the predicate method. Thoughts? Thanks.


In reply to Why is the predicate method not available for a MooseX::Privacy protected attribute? by paulymer

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.