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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |