Cannot redeclare Aws\constantly() (previously declared in /var/www/html/axcessio/aws_new/Aws/functions.php:19) - sugarcrm

$EC2client = EC2client::factory(array(
'credentials.cache' => $cacheAdapter,
'version' => '2016-11-15',
'region' => 'us-west-1',
));
Cannot redeclare Aws\constantly() (previously declared in /var/www/html/axcessio/aws_new/Aws/functions.php:19) in /var/www/html/microservices/vendor/aws/aws-sdk-php/src/functions.php on line 22

Related

How can I lift an attribute constraint depending on another attribute from a trigger into a type refinement?

This works:
use Moops;
class Foo :ro {
use Types::Common::Numeric qw(PositiveOrZeroInt);
has from => required => true, isa => PositiveOrZeroInt;
has to => required => true, isa => PositiveOrZeroInt, trigger => method($to) {
die 'must be from ≤ to' unless $self->from <= $to
};
}
Foo->new(from => 0, to => 1); # ok
Foo->new(from => 1, to => 0); # "must be from ≤ to at …"
I would like to be able to make the constraint part of the type, somehow.
use Moops;
class Bar :ro {
use Types::Common::Numeric qw(PositiveOrZeroInt);
has from => required => true, isa => PositiveOrZeroInt;
has to => required => true, isa => PositiveOrZeroInt->where(sub {
$self->from <= $_
});
}
Bar->new(from => 0, to => 1);
# Global symbol "$self" requires explicit package name
# (did you forget to declare "my $self"?)
I checked that the where sub only receives one parameter.
If you wanted to do it in a type check, you could combine the two attributes into one attribute which would be an arrayref holding both numbers.
use Moops;
class Foo :ro {
use Types::Standard qw(Tuple);
use Types::Common::Numeric qw(PositiveOrZeroInt);
has from_and_to => (
required => true,
isa => Tuple->of(PositiveOrZeroInt, PositiveOrZeroInt)->where(sub {
$_->[0] <= $_->[1];
}),
# Provide `from` and `to` methods to fetch values
handles_via => 'Array',
handles => {
'from' => [ get => 0 ],
'to' => [ get => 1 ],
},
);
# Allow `from` and `to` to be separate in the constructor
method BUILDARGS {
my %args = ( #_==1 ? %{$_[0]} : #_ );
$args{from_and_to} ||= [ $args{from}, $args{to} ];
\%args;
}
}
Foo->new(from => 0, to => 1); # ok
Foo->new(from => 1, to => 0); # "must be from ≤ to at …"
I wouldn't do it in a type check though. I'd do it in BUILD (no, not BUILDARGS) if the attribute were read-only, or trigger if it were read-write.

How can one attribute's default sub access another attribute with Moose?

###############################################################################
# Attributes
###############################################################################
has 'primary_cluster' => (
is => 'ro',
isa => 'Str',
required => TRUE,
);
has 'secondary_cluster' => (
is => 'ro',
isa => 'Str',
required => FALSE,
default => sub {$_[0]->primary_cluster},
);
has '_connection' => (
is => 'ro',
required => FALSE,
init_arg => undef,
default => sub {
Core::mConnection->new(
primary_cluster => $_[0]->primary_cluster,
secondary_cluster => $_[0]->secondary_cluster,
);
},
);
I'm trying to have a private attribute _connection that uses other attributes to create a mConnection object. The problem I'm running into is that inside the default subroutine of _connection, $_[0]->primary_cluster is always undef. Is there any way to guarantee order to the attribute creation or is there a better way to go about doing this?
I do not want this attribute to be lazy; I need it to be created when the object is constructed.
The object is still being constructed! Delay your attribute's initialization until after it's constructed. the following delays its initialization until it's used:
lazy => 1
You could also use a BUILD method instead of a default.
sub BUILD {
my $self = shift;
$self->_connection(
Core::mConnection->new(
primary_cluster => $self->primary_cluster,
secondary_cluster => $self->secondary_cluster,
)
);
}
Of course, you'll need to make the attribute writable first.

Creating attribute defaults by calling a wrapped object

I have WrapperClass object that has an InnerClass object as an attribute. The InnerClass object has a weight attribute. My WrapperClass object also has a weight attribute and I want its default value to be whatever the value of the InnerClass object's weight attribute is.
#!/usr/bin/perl
package InnerClass;
use Moose;
has 'weight' => (
is => 'rw',
);
package WrapperClass;
use Moose;
has 'wrapped' => (
is => 'rw',
lazy => 1,
default => sub {InnerClass->new(weight => 1)},
);
has 'weight' => (
is => 'rw',
default => sub {
my $self = shift;
$self->wrapped->weight()
},
lazy => 1,
);
The code above works, but in reality InnerClass has many attributes which WrapperClass needs to do the same thing for. Ideally I would do something like this when I'm writing WrapperClass:
use Moose;
has 'wrapped' => (
is => 'rw',
);
my #getDefaultsFromWrappers
= qw(weight height mass x y z label); # etc ...
foreach my $attr (#getDefaultsFromWrappers) {
has $attr => (
is => 'rw',
default => sub {
# Somehow tell the default which attribute
# it needs to call from wrapped object?
my $self = shift;
$self->wrapped->???()
},
lazy => 1,
);
}
However, there is no way of passing an argument to a default or builder to tell it which attribute it is building. I've considered using caller but this seems like a hack.
Does anyone know how I could accomplish this style of attribute declaration or is it a case of declaring each attribute and its default separately?
You can use $attr where your question marks are because it is still in scope when you declare the attributes.
foreach my $attr (#getDefaultsFromWrappers) {
has $attr => (
is => 'rw',
default => sub { shift->wrapped->$attr() },
lazy => 1,
);
}
The following is a possible alternative, which you might want to use if your attribute declarations are not uniform:
has weight => (
is => 'rw',
isa => 'Num',
default => _build_default_sub('weight'),
lazy => 1,
);
has label => (
is => 'rw',
isa => 'Str',
default => _build_default_sub('label'),
lazy => 1,
);
sub _build_default_sub {
my ($attr) = #_;
return sub { shift->wrapped->$attr };
}
This may be better handled by method delegation and default values in the inner object.
With these, the example you gave can be better written as:
#!/usr/bin/perl
use strict;
use warnings;
package InnerClass;
use Moose;
has weight => (
is => 'rw',
default => 1,
);
package WrapperClass;
use Moose;
has wrapped => (
is => 'rw',
isa => 'InnerClass',
lazy => 1,
default => sub { InnerClass->new },
handles => [ 'weight' ],
);
package main;
my $foo = WrapperClass->new;
print $foo->weight;
Any additional defaults would be added as default on the InnerClass, and within the WrapperClass, add to wrapped 'handles' array ref to indicate that it should be delegated to that object.
If don't want the defaults to be applied to all instances of InnerClass, then you can remove the default from there, specify all attributes required (to give better error detection), and specify all attributes in the default constructor.

using localtime inside moose default values

What's wrong with the code below ? When run, I get: "Use of uninitialized value in concatenation (.) or string at ./main.pl line 14"
#!/usr/bin/perl
package Test;
use Moose;
has 'message' => (isa => 'HashRef', is => 'ro', default => sub{{(localtime)[2] => {(localtime)[3] => "hello"}}});
# the one below works fine
#has 'message' => (isa => 'HashRef', is => 'ro', default => sub{{"18" => {"16" => "hello"}}});
sub show {
my $self = shift;
print("Test: " . $self->message->{(localtime)[2]}->{(localtime)[3]} . "\n");
}
my $o = Test->new();
$o->show();
If I do not use localtime() then it works fine. Also localtime[2] and [3] do not change very often (2 is hours, 3 is month day) so the problem is not that. If I run the script with a debugger, I get:
x $self
0 Test=HASH(0x3597300)
'message' => HASH(0x3597618)
16 => 'hello'
So it looks like I 'lose' one level of indirection, not really sure why... Any idea ?
The outer {} do not parse as a hashref. Add an explicit return:
has 'message' => (isa => 'HashRef', is => 'ro', default => sub{ return {(localtime)[2] => {(localtime)[3] => "hello"}} });
A + to force this works, too.
has 'message' => (isa => 'HashRef', is => 'ro', default => sub{ +{(localtime)[2] => {(localtime)[3] => "hello"}} });

Defining a MX::Declare method as a attribute trigger

The following code works as I'd expect. The cached lazy attribute gets cleared and rebuilt when the foo attribute it depends on is changed.
use MooseX::Declare;
use 5.010;
class Test {
has foo => ( isa => 'Str', is => 'rw', trigger => sub {my $self = shift; $self->clearer}, default => '' );
has lazy => ( isa => 'Str', is => 'ro', lazy => 1, clearer => 'clearer',
default => method { say 'building lazy'; return "foo is '".$self->foo."'"; },
);
method say ( ) {
say $self->lazy;
}
}
my $inst = Test->new( foo => 'baz' );
$inst->say;
$inst->say;
say $inst->foo();
$inst->foo('bar');
$inst->say;
output:
building lazy
foo is 'baz'
foo is 'baz'
baz
building lazy
foo is 'bar'
How do I, however, use the MX::Declare sugar for the trigger subroutine? Defining foo as:
has foo => ( isa => 'Str', is => 'rw', trigger => method {$self->clearer}, default => '' );
Results in the class dying on compilation (below). Am I doing something wrong with my anonymous method declaration?
Trigger must be a CODE ref on attribute (foo) at
C:/Strawberry/perl/site/lib/Moose/Meta/Attribute.pm line 423
Moose::Meta::Attribute::_process_trigger_option('Moose::Meta::Attribute',
'foo', 'HASH(0x2a5d14c)') called at C:
/Strawberry/perl/site/lib/Moose/Meta/Attribute.pm line 299
Moose::Meta::Attribute::_process_options('Moose::Meta::Attribute',
'foo', 'HASH(0x2a5d14c)') called at C:/Strawb
erry/perl/site/lib/Moose/Meta/Attribute.pm line 88
Moose::Meta::Attribute::new('Moose::Meta::Attribute', 'foo', 'trigger', 'MooseX::Method::Signatures::Meta::Metho
d=HASH(0x39a421c)', 'isa', 'Str', 'definition_context',
'HASH(0x3452184)', 'default', '', 'is', 'rw') called at C:/Straw
berry/perl/site/lib/Moose/Meta/Attribute.pm line 114
Moose::Meta::Attribute::interpolate_class_and_new('Moose::Meta::Attribute',
'foo', 'trigger', 'MooseX::Method::S
ignatures::Meta::Method=HASH(0x39a421c)', 'isa', 'Str', 'default', '',
'definition_context', 'HASH(0x3452184)', 'is', 'r w') called at
C:/Strawberry/perl/site/lib/Moose/Meta/Class.pm line 704
Moose::Meta::Class::_process_new_attribute('Moose::Meta::Class=HASH(0x38c79d4)',
'foo', 'trigger', 'MooseX::Meth
od::Signatures::Meta::Method=HASH(0x39a421c)', 'isa', 'Str',
'default', '', 'definition_context', 'HASH(0x3452184)', 'is ', 'rw')
called at C:/Strawberry/perl/site/lib/Moose/Meta/Class.pm line 697
Moose::Meta::Class::_process_attribute('Moose::Meta::Class=HASH(0x38c79d4)',
'foo', 'trigger', 'MooseX::Method::
Signatures::Meta::Method=HASH(0x39a421c)', 'isa', 'Str', 'default',
'', 'definition_context', 'HASH(0x3452184)', 'is', ' rw') called at
C:/Strawberry/perl/site/lib/Moose/Meta/Class.pm line 566
Moose::Meta::Class::add_attribute('Moose::Meta::Class=HASH(0x38c79d4)',
'foo', 'trigger', 'MooseX::Method::Signa
tures::Meta::Method=HASH(0x39a421c)', 'isa', 'Str', 'default', '',
'definition_context', 'HASH(0x3452184)', 'is', 'rw') called at
C:/Strawberry/perl/site/lib/Moose.pm line 77
Moose::has('Moose::Meta::Class=HASH(0x38c79d4)', 'foo', 'isa', 'Str', 'is', 'rw', 'trigger', 'MooseX::Method::Si
gnatures::Meta::Method=HASH(0x39a421c)', 'default', '') called at
C:/Strawberry/perl/site/lib/Moose/Exporter.pm line 356
Moose::has('foo', 'isa', 'Str', 'is', 'rw', 'trigger', 'MooseX::Method::Signatures::Meta::Method=HASH(0x39a421c) ',
'default', '') called at mx_declare.pl line 5
main::ANON() called at C:/Strawberry/perl/site/lib/MooseX/Declare/Syntax/MooseSetup.pm line
81
MooseX::Declare::Syntax::MooseSetup::ANON('CODE(0x38c3a94)')
called at mx_declare.pl line 13
The method keyword returns an instance of the MooseX::Method::Signatures::Meta::Method class, which is a subclass of Moose::Meta::Method, which is a subclass of Class::MOP::Method.
Moose allows a method object for default, but not for trigger, which must be a regular coderef.
If you really want to use the method keyword there, you could probably do:
trigger => method { $self->clearer }->body,
But it's probably easier (and saner) to do what #cjm suggests and just use a regular coderef:
trigger => sub { shift->clearer },
You can't. method returns an object, not a plain coderef. However, you can write this even more concisely than method would allow:
has foo => ( isa => 'Str', is => 'rw', trigger => sub {shift->clearer}, default => '' );
That's 3 characters shorter than method {$self->clearer}. And it has less overhead.