3

Вопрос по perl – Как я могу предоставить альтернативный аргумент init для атрибута в Moose?

Error: User Rate Limit Exceededinit_argError: User Rate Limit Exceeded

package Test {
    use Moose;
    has attr => (
       is => 'ro',
       isa => 'Str',
       init_arg => 'attribute'
    );
}

Error: User Rate Limit Exceeded

Test->new({ attribute => 'foo' });

Error: User Rate Limit Exceeded

Test->new({ attr => 'foo' });

Error: User Rate Limit Exceeded

Error: User Rate Limit ExceededError: User Rate Limit Exceeded

updateError: User Rate Limit Exceededaround initialize_instance_slotError: User Rate Limit Exceeded

updateError: User Rate Limit ExceededBUILDARGSError: User Rate Limit ExceededlabelError: User Rate Limit ExceededError: User Rate Limit ExceededError: User Rate Limit Exceeded

has attr => (
   is => 'ro',
   isa => 'Str',
   alt_init_arg => 'attribute'
);

update

Error: User Rate Limit Exceeded

use 5.014;
use warnings;

package MooseX::Meta::Attribute::Trait::OtherName {
    use Moose::Role;
    use Carp;

    has other_name => (
        isa       => 'Str',
        predicate => 'has_other_name',
        required  => 1,
        is        => 'ro',
    );

    around initialize_instance_slot => sub {
        my $orig = shift;
        my $self = shift;

        my ( $meta_instance, $instance, $params ) = @_;

        confess 'actually calling this code';

        return $self->$orig(@_)
            unless $self->has_other_name && $self->has_init_arg;

        if ( $self->has_other_name ) {
            $params->{ $self->init_arg }
                = delete $params->{ $self->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 => (
        traits    => [ 'OtherName' ],
        is        => 'ro',
        isa       => 'Str',
        other_name => 'Attr',
    );

    __PACKAGE__->meta->make_immutable;
}

package Client {
    use Moose;

    sub serialize {
        my ( $self, $message ) = @_;

        confess 'no message' unless defined $message;

        my %h;
        foreach my $attr ( $message->meta->get_all_attributes ) {
            if (
                    $attr->does('MooseX::Meta::Attribute::Trait::OtherName')
                    && $attr->has_other_name
                ) {
                $h{$attr->other_name} = $attr->get_value( $message );
            }
        }
        return \%h;
    }
    __PACKAGE__->meta->make_immutable;
}

my $message = Message->new( Attr => 'foo' );

my $ua = Client->new;

my %h = %{ $ua->serialize( $message )};

use Data::Dumper::Concise;

say Dumper \%h

Error: User Rate Limit ExceededaroundError: User Rate Limit Exceeded

  • Error: User Rate Limit Exceededgithub.com/jberger/MooseX-RememberHistory/blob/master/lib/…Error: User Rate Limit Exceeded

    от
  • Error: User Rate Limit Exceeded

    от xenoterracide
  • Error: User Rate Limit Exceeded

    от xenoterracide
  • Error: User Rate Limit Exceeded

    от
  • Error: User Rate Limit Exceeded

    от
  • Error: User Rate Limit Exceeded

    от xenoterracide
  • Error: User Rate Limit Exceeded

    от xenoterracide
  • Error: User Rate Limit Exceeded

    от Ether
  • Error: User Rate Limit Exceeded

    от xenoterracide
  • Error: User Rate Limit Exceeded

    от xenoterracide
  • Error: User Rate Limit Exceeded

    от Ether
  • 0

    Error: User Rate Limit Exceeded

    At construction time, an attribute should be able to be set by its init_arg and any alternate init_args defined on the attribute. An attribute should not be able to be manipulated by its alternate init_args except at instance construction; that is, aside from the above, the attribute should behave "normally".

  • 3

    Error: User Rate Limit Exceeded

    #!/usr/bin/env perl
    
    use strict;
    use warnings;
    
    {
      package MyClass;
    
      use Moose;
      has attr => (
         is => 'ro',
         isa => 'Str',
         required => 1,
      );
    
      around BUILDARGS => sub {
        my $orig = shift;
        my $self = shift;
        my %args = ref $_[0] ? %{shift()} : @_;
    
        if (exists $args{attribute}) {
          $args{attr} = delete $args{attribute};
        }
    
        $self->$orig(%args);
      };
    }
    
    my $one = MyClass->new(attribute => "Hi");
    my $two = MyClass->new(attr => "Bye");
    
    print $one->attr, "\n";
    print $two->attr, "\n";
    

  • 3

    MooseX::AliasesError: User Rate Limit ExceededMooseX::AliasesError: User Rate Limit Exceeded

    Error: User Rate Limit ExceededMooseX::AliasesError: User Rate Limit ExceededMoose::Util::meta_attribute_aliasError: User Rate Limit ExceededMoose::Meta::Attribute::Custom::Trait::OtherNameError: User Rate Limit ExceededMoose::Util::meta_attribute_alias 'OtherName';Error: User Rate Limit Exceeded

    Error: User Rate Limit Exceeded

    Error: User Rate Limit ExceededError: User Rate Limit Exceeded.

    Error: User Rate Limit Exceeded

    package MooseX::AltInitArg::Meta::Trait::Attribute;
    use Moose::Role;
    use namespace::autoclean;
    Moose::Util::meta_attribute_alias 'AltInitArg';
    
    
    has alt_init_arg => (
        is         => 'ro',
        isa        => 'Str',
        predicate  => 'has_alt_init_arg',
    );
    
    
    around initialize_instance_slot => sub {
        my $orig = shift;
        my $self = shift;
        my ($meta_instance, $instance, $params) = @_;
    
        return $self->$orig(@_)
            # don't run if we haven't set any alt_init_args
            # don't run if init_arg is explicitly undef
            unless $self->has_alt_init_arg && $self->has_init_arg;
    
        if (my @alternates = grep { exists $params->{$_} } ($self->alt_init_arg)) {
            if (exists $params->{ $self->init_arg }) {
                push @alternates, $self->init_arg;
            }
    
            $self->associated_class->throw_error(
                'Conflicting init_args: (' . join(', ', @alternates) . ')'
            ) if @alternates > 1;
    
            $params->{ $self->init_arg } = delete $params->{ $alternates[0] };
        }
        $self->$orig(@_);
    };
    
    1;
    __END__
    

    Error: User Rate Limit Exceeded

    package MooseX::AltInitArg::Meta::Trait::Class;
    use Moose::Role;
    use namespace::autoclean;
    
    around _inline_slot_initializer => sub {
        my $orig = shift;
        my $self = shift;
        my ($attr, $index) = @_;
    
        my @orig_source = $self->$orig(@_);
        return @orig_source
            # only run on aliased attributes
            unless $attr->meta->can('does_role')
                && $attr->meta->does_role('MooseX::AltInitArg::Meta::Trait::Attribute');
        return @orig_source
            # don't run if we haven't set any aliases
            # don't run if init_arg is explicitly undef
            unless $attr->has_alt_init_arg && $attr->has_init_arg;
    
        my $init_arg = $attr->init_arg;
    
        return (
            'if (my @aliases = grep { exists $params->{$_} } (qw('
              . $attr->alt_init_arg . '))) {',
                'if (exists $params->{' . $init_arg . '}) {',
                    'push @aliases, \'' . $init_arg . '\';',
                '}',
                'if (@aliases > 1) {',
                    $self->_inline_throw_error(
                        '"Conflicting init_args: (" . join(", ", @aliases) . ")"',
                    ) . ';',
                '}',
                '$params->{' . $init_arg . '} = delete $params->{$aliases[0]};',
            '}',
            @orig_source,
        );
    };
    1;
    __END__
    

    Error: User Rate Limit ExceededMoose::ExporterError: User Rate Limit Exceeded

    package MooseX::AltInitArg;
    use Moose();
    
    use Moose::Exporter;
    use MooseX::AltInitArg::Meta::Trait::Attribute;
    
    Moose::Exporter->setup_import_methods(
        class_metaroles => { class => ['MooseX::AltInitArg::Meta::Trait::Class'] }
    );
    
    1;
    __END__
    

    Error: User Rate Limit Exceeded

    package MyApp;
    use 5.10.1;
    use Moose;
    use MooseX::AltInitArg;
    
    has foo => (
        is            => 'ro',
        traits        => ['AltInitArg'],
        alt_init_arg => 'bar',
    );
    
    
    my $obj = MyApp->new( bar => 'bar' );
    say $obj->foo; # prints bar
    

    Error: User Rate Limit Exceeded

    Error: User Rate Limit Exceeded