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

=?utf-8?Q?Re=3A_Wrap_an_attribute=E2=80=99s_=5Eget=5Fvalue_method?==?utf-8?Q?_in_TWEAK?=

Thread Next
From:
Vadim Belman
Date:
September 8, 2019 18:57
Subject:
=?utf-8?Q?Re=3A_Wrap_an_attribute=E2=80=99s_=5Eget=5Fvalue_method?==?utf-8?Q?_in_TWEAK?=
Message ID:
BD35C7E2-BDCE-4A95-81AA-C7878139DF34@lflat.org
Hi Mark,

get_value won't give you what you expect. It's use is narrowly limited to trait 'is handles' and method .perl. Besides, are you sure all of your attributes will be considered as belonging to XML? I mean, won't there be need for utility attributes serving exclusively internal needs of a class? If so, then the best solution is to create a trait for an attribute and have it like:

has $.SerialNumber is xmlattr;

The trait would then install an accessor which would do what you need.

Best regards,
Vadim Belman

> On Sep 8, 2019, at 12:41 PM, Mark Devine <mark@markdevine.com> wrote:
> 
> 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 ' ~ $attr.name ~ "'s get_value"; nextsame; };
>         }
>         self;
>     }
> }
> say System.new(:SerialNumber('ABCDEFG')).SerialNumber;
>  
> # 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?
>  
> Thanks,
>  
> Mark


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About