develooper Front page | perl.perl6.users | Postings from September 2019

Mark Devine
September 8, 2019 16:41

Message ID:
Perl6 Community,

How do I properly wrap an attribute’s ^get_value method in TWEAK?  If a condition exists, I’d like to wrap all (:local) attributes so that they can do some extra work.

The module that I’m working on has classes/attributes for hundreds of fields in dozens of different, big XML files -- there’s a lot.  And XML is easily extended, so I may very well be adding hundreds more attributes in the future.  I would very much prefer to preserve the automatic accessor behavior of ‘has $.attr;’ versus writing a billion set/get accessors myself – truly prohibitive.  Wrapping seems the elegant/extensible approach.

class System {
    has $.SerialNumber;
    submethod TWEAK {
        for self.^attributes(:local) -> $attr {
            self.$attr.^get_value.wrap: { say 'in ' ~ $ ~ "'s get_value"; nextsame; };

# OUTPUT: No such method 'CALL-ME' for invocant of type 'Attribute'

I know the above .wrap is probably misguided, but it sort of demonstrates what I’m trying to do.

I’m not finding the proper approach.  Any hints?  Any tutorials?  Any existing examples in the ecosystem that I could mimic?


Mark Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About