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

Dear Perlmonks, I have to ask for your wisdom. I am currently writing a OO-Module in Mouse and came into trouble using predicates.

The Situation is as follows:
My Module is an extension to an already existing (huge legacy) deamon, written in procedural perl. This deamon instantiates an object of my Class and hands over a parameter hash in the constructor, to fill the objects accessors. Like this:

\%data = { customer_number => $custno, country => $country, ticket_typ +e => $ticketsystem }; $test_class = "Utils::Testclass::MyTest"->new($data); <\code> <p>Utils::Testclass::MyTest has correspondig accessors like this:</p> <code> has 'customer_number' => ( is => 'rw', reader => 'get_customer_number', writer => 'set_customer_number', predicate => 'has_customer_number', clearer => 'clear_customer_number', ); has 'country' => ( is => 'rw', reader => 'get_country', writer => 'set_country', predicate => 'has_country', clearer => 'clear_country', ); has 'ticket_type' => ( is => 'rw', reader => 'get_ticket_type', writer => 'set_ticket_type', predicate => 'has_ticket_type', clearer => 'clear_ticket_type', );

I wish to test if my accessors are set using my perdicate e.g. $self->has_country and based on which accessors are set the module does some internal magic. So far, nothing special, but my code did not work. Then I found this gem in the deamons code, which replaces all values which are undef with empty strings and kills my usage of predicates:

map { $data{$_} = '' unless defined $data{$_} } keys %data;

Note: It is not possible for me to change the code of the deamon. That program expects the empty string several times and I do not have permission to change that.

What can I do to get my predicates working under these conditions?

I already had the following ideas:

  1. performing if( not definded($self->get_country) || $self->get_country eq ''). Works, but is far from elegant and not the Mouse/Moose-OO-way. I'd prefer to use the predicate.
  2. I thought of the following trigger, to check my values before writing them to the accessor and eventually calling the clearer:
    has 'country' => ( is => 'rw', reader => 'get_country', writer => 'set_country', predicate => 'has_country', clearer => 'clear_country', trigger => sub { my ($self) = @_; if ( $self->get_country eq '' || not defined($sel +f->get_country) ) { $self->clear_country; } } );
    This is much more the direction that I want and I can use the predicate. However: I have to copy the exact same trigger method for every accessor, changing only the name of the get and clear methods. I really don't like this duplicate code, as there are going to be more accessors like these in the future.

Dear Perlmonks, do you have a more elegant idea on this topic? Anything with less duplicate code not throwing overboard the Mouse-OO-way?

I appreciate your thoughts,