Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I provide an alternate init arg for an attribute in Moose?
    primarykey
    data
    text
    <p>I of course know that I can rename the init arg for an attribute by setting <code>init_arg</code> (e.g) </p> <pre><code>package Test { use Moose; has attr =&gt; ( is =&gt; 'ro', isa =&gt; 'Str', init_arg =&gt; 'attribute' ); } </code></pre> <p>which would allow me to</p> <pre><code>Test-&gt;new({ attribute =&gt; 'foo' }); </code></pre> <p>but not</p> <pre><code>Test-&gt;new({ attr =&gt; 'foo' }); </code></pre> <p>at the same time</p> <p><a href="https://metacpan.org/module/MooseX%3a%3aAliases" rel="nofollow">MooseX::Aliases</a> actually has this behavior, but creating an alias also creates accessors. I'm currently trying to understand the code in that module to see if I can't determine how it does it, so that I can replicate said functionality (in a way I understand). If someone could explain how to do it here with an example that'd be great.</p> <p><em>update</em> it appears that MX::Aliases is doing this by way of replacing what's actually passed to the constructor in an <code>around initialize_instance_slot</code> but I'm still not sure how that's actually getting called, because in my test code my around isn't actually getting executed.</p> <p><em>update</em> munging in <code>BUILDARGS</code> isn't really an option because what I'm trying to do allow setting of the accessor via the name of the <em>label</em> I'm adding to the attribute via <a href="https://metacpan.org/module/Moose%3a%3aCookbook%3a%3aMeta%3a%3aRecipe3" rel="nofollow">Meta Recipe3</a>. You might say I'm doing</p> <pre><code>has attr =&gt; ( is =&gt; 'ro', isa =&gt; 'Str', alt_init_arg =&gt; 'attribute' ); </code></pre> <p><em>update</em></p> <p>here's what I've managed to work out with what I'm trying to do so far.</p> <pre><code>use 5.014; use warnings; package MooseX::Meta::Attribute::Trait::OtherName { use Moose::Role; use Carp; has other_name =&gt; ( isa =&gt; 'Str', predicate =&gt; 'has_other_name', required =&gt; 1, is =&gt; 'ro', ); around initialize_instance_slot =&gt; sub { my $orig = shift; my $self = shift; my ( $meta_instance, $instance, $params ) = @_; confess 'actually calling this code'; return $self-&gt;$orig(@_) unless $self-&gt;has_other_name &amp;&amp; $self-&gt;has_init_arg; if ( $self-&gt;has_other_name ) { $params-&gt;{ $self-&gt;init_arg } = delete $params-&gt;{ $self-&gt;other_name }; } }; } package Moose::Meta::Attribute::Custom::Trait::OtherName { sub register_implementation { 'MooseX::Meta::Attribute::Trait::OtherName' } } package Message { use Moose; # use MooseX::StrictConstructor; has attr =&gt; ( traits =&gt; [ 'OtherName' ], is =&gt; 'ro', isa =&gt; 'Str', other_name =&gt; 'Attr', ); __PACKAGE__-&gt;meta-&gt;make_immutable; } package Client { use Moose; sub serialize { my ( $self, $message ) = @_; confess 'no message' unless defined $message; my %h; foreach my $attr ( $message-&gt;meta-&gt;get_all_attributes ) { if ( $attr-&gt;does('MooseX::Meta::Attribute::Trait::OtherName') &amp;&amp; $attr-&gt;has_other_name ) { $h{$attr-&gt;other_name} = $attr-&gt;get_value( $message ); } } return \%h; } __PACKAGE__-&gt;meta-&gt;make_immutable; } my $message = Message-&gt;new( Attr =&gt; 'foo' ); my $ua = Client-&gt;new; my %h = %{ $ua-&gt;serialize( $message )}; use Data::Dumper::Concise; say Dumper \%h </code></pre> <p>problem is that my <code>around</code> block is never being run and I'm not sure why, maybe I'm wrapping it in the wrong place or something.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload