Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Autoclearing all accessors in a Mouse/Moose class

by choroba (Cardinal)
on Jan 29, 2016 at 13:13 UTC ( #1153992=note: print w/replies, xml ) Need Help??


in reply to Autoclearing all accessors in a Mouse/Moose class

has is a subroutine as any other. You can call it in a loop, you can pass variables as its arguments.
for my $attr (qw( country customer_number ticket_type )) { my $reader = "get_$attr"; my $clearer = "clear_$attr"; has $attr => ( is => 'rw', reader => $reader, writer => "set_$attr", predicate => "has_$attr", clearer => $clearer, trigger => sub { my ($self) = @_; if (! defined $self->$reader || $self->$reader eq '' ) + { $self->$clearer; } } ); }

Notice I switched the two conditions in the || to prevent warnings about undefined.

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^2: Autoclearing all accessors in a Mouse/Moose class
by yulivee07 (Sexton) on Jan 29, 2016 at 14:48 UTC

    Wow. Thats really clever. Mind is blown.
    I didn't even known I could stick has into subroutines like that. You didn't just solve my predicate problem, you also cleaned up my very similar accessors.
    Thanks a lot for your input!

    Yulivee

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1153992]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2023-12-03 16:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (20 votes). Check out past polls.

    Notices?