Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

OK, here's a massively generalized version of the above:

use 5.010; use strict; use warnings; BEGIN { package Trait::Attribute::Derived; no thanks; use MooseX::Role::Parameterized; use List::MoreUtils 'any'; use namespace::autoclean; use base do { package Trait::Attribute::Derived::__CLASS_METHODS__; use Sub::Install 'install_sub'; use namespace::autoclean; my @saved; sub make_trait { my ($pkg, %args) = @_; push @saved, $pkg->meta->generate_role(parameters => \%arg +s); return $saved[-1]->name; } sub import { my $pkg = shift; my $caller = caller; while (@_) { my $name = shift; my $trait = $pkg->make_trait(%{+shift}); install_sub { into => $caller, as => $name, code => sub () { $trait }, } } } __PACKAGE__; }; parameter processor => (is => 'ro', required => 1, isa => 'Code +Ref'); parameter fields => (is => 'ro', required => 1, isa => 'Hash +Ref'); parameter is => (is => 'ro', default => 'ro', isa => 'Str' +); parameter source => (is => 'ro', required => 1, isa => 'Str' +); role { my $p = shift; my @fields = keys %{ $p->fields }; has postprocessor => (is => 'ro', isa => 'CodeRef'); for my $attr (@fields) { has $attr => (is => 'ro', isa => $p->fields->{$attr}); } before _process_options => sub { my ($meta, $name, $spec) = @_; $spec->{is} //= $p->is; $spec->{lazy} //= 1; $spec->{builder} //= "_build_$name"; }; after attach_to_class => sub { my $attr = shift; my $class = $attr->associated_class; return if $class->has_method($attr->builder); my $source = $p->source; my $processor = $p->processor; my $postprocess = $attr->postprocessor; my %data = map { ; $_ => $attr->$_ } @fields; $class->add_method($attr->builder, sub { my $self = shift; local %_ = %data; local $_ = $self->$source; $_ = $self->$processor($_, +{%data}); return $_ unless $postprocess; return $self->$postprocess($_, +{%data}); }); }; }; }; { package Person; use Moose; use Trait::Attribute::Derived Split => { source => 'full_name', fields => { segment => 'Num' }, processor => sub { (split)[$_{segment}] }, }; has full_name => (is => 'ro', isa => 'Str'); has first_name => (traits => [Split], segment => +0); has initial => (traits => [Split], segment => +0, postprocessor + => sub { substr $_, 0, 1 }); has last_name => (traits => [Split], segment => -1); } my $bob = Person->new(full_name => 'Robert Redford'); say $bob->first_name; say $bob->initial; say $bob->last_name;

If you can understand that and how to apply it to your problem, then you're a true metahacker! :-)

Update: this is now on CPAN as Trait::Attribute::Derived.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

In reply to Re^2: Binary data structure to moose class. by tobyink
in thread Binary data structure to moose class. by chrestomanci

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (9)
    As of 2014-12-28 08:03 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (179 votes), past polls