Using Moose Before Changing Conflict Conflict Method Arguments

As mentioned earlier today , I'm trying to set the attributes of an instance when calling one of its methods. I also want this attribute to be private. As already indicated, I cannot set this attribute to ro, since it also prohibits read-access from the class. So now I install it on rw, but I started using the module MooseX::Privacy. So my attribute declaration is as follows:

has 'grow_params' => (
  is  => 'rw',
  isa => 'HashRef',
  traits => [qw/Private/],
);

This works fine if I just do something like this:

sub grow {
  my ($self, $params) = @_;
  $self->grow_params($params);
}

. , - before. , , .

, before:

before 'grow_params' => sub {
  my ($self, $params) = @_;
  # Setting default value
  $params->{'overripe'} = exists $params->{'overripe'} ? $params->{'overripe'} : 0;
  # Making sure its boolean
  confess "Argument 'overripe' has to be 0 or 1"
    unless ($params->{'overripe'} == 0 || $params->{'overripe'} == 1);
};

:

Attribute grow_params is private at C:/strawberry/perl/site/lib/MooseX/Privacy/Meta/Attribute/Private.pm line 13.

( Protected, , , , , .) , , ? , , Banana, - ?

Attribute grow_params is private at c:/strawberry/perl/site/lib/MooseX/Privacy/Meta/Attribute/Private.pm line 13.
        MooseX::Privacy::Meta::Attribute::Private::_check_private(Moose::Meta::Class::__ANON__::SERIAL::9=HASH(0x3fd4a00), "Class::MOP::Method::Wrapped", "grow_params", "Banana", "Banana") called at c:/strawberry/perl/site/lib/MooseX/Privacy/Meta/Attribute/Privacy.pm line 31
        Banana::grow_params(Banana=HASH(0x25223e8), HASH(0xed97a8)) called at c:/strawberry/perl/site/lib/Class/MOP/Method/Wrapped.pm line 44
        Banana::_wrapped_grow_params(Banana=HASH(0x25223e8), HASH(0xed97a8)) called at c:/strawberry/perl/site/lib/Class/MOP/Method/Wrapped.pm line 95
        Banana::grow_params(Banana=HASH(0x25223e8), HASH(0xed97a8)) called at C:\xampp\htdocs\grinding\banana\/Banana.pm line 31
        Banana::grow(Banana=HASH(0x25223e8), HASH(0xed97a8)) called at run.pl line 16

.

# Banana.pm
package Banana;

use strict;
use warnings;
use Carp qw( confess );

use Moose;
use MooseX::Privacy;

has ['peel', 'edible'] => (
  is  => 'ro',
  isa => 'Bool',
  required => 1,
);

has 'color' => (
  is  => 'ro',
  isa => 'Str',
  required => 1,
);

has 'grow_params' => (
  is  => 'rw',
  isa => 'HashRef',
  traits => [qw/Private/],
);

sub grow {
  my ($self, $params) = @_;
  $self->grow_params($params);
}

before 'grow_params' => sub {
  my ($self, $params) = @_;
  # Setting default value
  $params->{'overripe'} = exists $params->{'overripe'} ? $params->{'overripe'} : 0;
  # Making sure its boolean
  confess "Argument 'overripe' has to be 0 or 1"
    unless ($params->{'overripe'} == 0 || $params->{'overripe'} == 1);
};

1;

# run.pl
use strict;
use warnings;

use File::Basename qw(fileparse dirname);
use Cwd qw(abs_path);

# Add current directory to @INC
use lib (fileparse(abs_path($0)))[1];

use Banana;

my $chiquita = Banana->new({
  peel => 1,
  edible => 0,
  color => 'green'
});

$chiquita->grow({
  size => 'medium',
  color => 'yellow'
});
+4
1

MooseX::Privacy, MooseX:: :: Meta:: Attribute:: Private v0.05, , .

sub _check_private {
    my ($meta, $caller, $attr_name, $package_name) = @_;
    confess "Attribute " . $attr_name . " is private"
        unless $caller eq $package_name;
}

, , $caller $package_name. Perl, .

before? $caller . :

Class::MOP::Method::Wrapped

, ( ).

, , MooseX::Privacy .

+3

Source: https://habr.com/ru/post/1682139/


All Articles