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,
Yulivee


In reply to Autoclearing all accessors in a Mouse/Moose class by yulivee07

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.