Moo object extends order The Next CEO of Stack OverflowHow can I fix Net::Amazon::MechanicalTurk's failing tests?Moose & isa ArrayRefDefining a MX::Declare method as a attribute triggerCalling an object method from array of objects in perl using mooseHow do I localize an object that is inside a property of a Moo object in Perl?Perl: using Class::Struct vs explicit Perl object referencePerl 5: namespace issues when `use`ing SWIG-generated module in declared packageWhat is @$ in perl?Perl: How to call a specific method in multiple inheritance?Movable Type: Can't locate object method “new” via package "HTTP::Request
Written every which way
Is 'diverse range' a pleonastic phrase?
How did the Bene Gesserit know how to make a Kwisatz Haderach?
How do I make a variable always equal to the result of some calculations?
What benefits would be gained by using human laborers instead of drones in deep sea mining?
If a black hole is created from light, can this black hole then move at speed of light?
What can we do to stop prior company from asking us questions?
Why has the US not been more assertive in confronting Russia in recent years?
Would a galaxy be visible from outside, but nearby?
sp_blitzCache results Memory grants
Interfacing a button to MCU (and PC) with 50m long cable
Is there an analogue of projective spaces for proper schemes?
Make solar eclipses exceedingly rare, but still have new moons
Calculus II Question
Do I need to enable Dev Hub in my PROD Org?
Is it professional to write unrelated content in an almost-empty email?
Novel about a guy who is possessed by the divine essence and the world ends?
Is it possible to search for a directory/file combination?
To not tell, not take, and not want
How do I go from 300 unfinished/half written blog posts, to published posts?
Why do airplanes bank sharply to the right after air-to-air refueling?
Why does standard notation not preserve intervals (visually)
Bold, vivid family
What happened in Rome, when the western empire "fell"?
Moo object extends order
The Next CEO of Stack OverflowHow can I fix Net::Amazon::MechanicalTurk's failing tests?Moose & isa ArrayRefDefining a MX::Declare method as a attribute triggerCalling an object method from array of objects in perl using mooseHow do I localize an object that is inside a property of a Moo object in Perl?Perl: using Class::Struct vs explicit Perl object referencePerl 5: namespace issues when `use`ing SWIG-generated module in declared packageWhat is @$ in perl?Perl: How to call a specific method in multiple inheritance?Movable Type: Can't locate object method “new” via package "HTTP::Request
Give the below code, it appears that the order you instatiate the objects matter. The below code will print the same list for both objects when i would expect a different list for each because list is an instance attribute that is created at BUILD time.
package t;
use Moo;
use Types::Standard qw(ArrayRef);
my @list = qw/foo bar baz/;
has list => (
is => 'rw',
isa => ArrayRef,
default => sub @list
);
1;
---
package u;
use Moo;
use Types::Standard qw(ArrayRef);
extends 't';
sub BUILD
my ($self) = @_;
push @$self->list(), qw/apple banana/;
return $self;
1;
---
#!perl
use Data::Printer;
use t;
use u;
my $u = u->new();
p $u->list();
my $t = t->new();
p $t->list();
Current Output:
[
[0] "foo",
[1] "bar",
[2] "baz",
[3] "apple",
[4] "banana"
]
[
[0] "foo",
[1] "bar",
[2] "baz",
[3] "apple",
[4] "banana"
]
Expected output:
[
[0] "foo",
[1] "bar",
[2] "baz",
[3] "apple",
[4] "banana"
]
[
[0] "foo",
[1] "bar",
[2] "baz"
]
perl
add a comment |
Give the below code, it appears that the order you instatiate the objects matter. The below code will print the same list for both objects when i would expect a different list for each because list is an instance attribute that is created at BUILD time.
package t;
use Moo;
use Types::Standard qw(ArrayRef);
my @list = qw/foo bar baz/;
has list => (
is => 'rw',
isa => ArrayRef,
default => sub @list
);
1;
---
package u;
use Moo;
use Types::Standard qw(ArrayRef);
extends 't';
sub BUILD
my ($self) = @_;
push @$self->list(), qw/apple banana/;
return $self;
1;
---
#!perl
use Data::Printer;
use t;
use u;
my $u = u->new();
p $u->list();
my $t = t->new();
p $t->list();
Current Output:
[
[0] "foo",
[1] "bar",
[2] "baz",
[3] "apple",
[4] "banana"
]
[
[0] "foo",
[1] "bar",
[2] "baz",
[3] "apple",
[4] "banana"
]
Expected output:
[
[0] "foo",
[1] "bar",
[2] "baz",
[3] "apple",
[4] "banana"
]
[
[0] "foo",
[1] "bar",
[2] "baz"
]
perl
add a comment |
Give the below code, it appears that the order you instatiate the objects matter. The below code will print the same list for both objects when i would expect a different list for each because list is an instance attribute that is created at BUILD time.
package t;
use Moo;
use Types::Standard qw(ArrayRef);
my @list = qw/foo bar baz/;
has list => (
is => 'rw',
isa => ArrayRef,
default => sub @list
);
1;
---
package u;
use Moo;
use Types::Standard qw(ArrayRef);
extends 't';
sub BUILD
my ($self) = @_;
push @$self->list(), qw/apple banana/;
return $self;
1;
---
#!perl
use Data::Printer;
use t;
use u;
my $u = u->new();
p $u->list();
my $t = t->new();
p $t->list();
Current Output:
[
[0] "foo",
[1] "bar",
[2] "baz",
[3] "apple",
[4] "banana"
]
[
[0] "foo",
[1] "bar",
[2] "baz",
[3] "apple",
[4] "banana"
]
Expected output:
[
[0] "foo",
[1] "bar",
[2] "baz",
[3] "apple",
[4] "banana"
]
[
[0] "foo",
[1] "bar",
[2] "baz"
]
perl
Give the below code, it appears that the order you instatiate the objects matter. The below code will print the same list for both objects when i would expect a different list for each because list is an instance attribute that is created at BUILD time.
package t;
use Moo;
use Types::Standard qw(ArrayRef);
my @list = qw/foo bar baz/;
has list => (
is => 'rw',
isa => ArrayRef,
default => sub @list
);
1;
---
package u;
use Moo;
use Types::Standard qw(ArrayRef);
extends 't';
sub BUILD
my ($self) = @_;
push @$self->list(), qw/apple banana/;
return $self;
1;
---
#!perl
use Data::Printer;
use t;
use u;
my $u = u->new();
p $u->list();
my $t = t->new();
p $t->list();
Current Output:
[
[0] "foo",
[1] "bar",
[2] "baz",
[3] "apple",
[4] "banana"
]
[
[0] "foo",
[1] "bar",
[2] "baz",
[3] "apple",
[4] "banana"
]
Expected output:
[
[0] "foo",
[1] "bar",
[2] "baz",
[3] "apple",
[4] "banana"
]
[
[0] "foo",
[1] "bar",
[2] "baz"
]
perl
perl
asked Mar 7 at 15:21
SparkeyGSparkeyG
431920
431920
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Since you mutate the array in question, you don't want a reference to the array that you use as the default @list
, you want to take a shallow copy [@list]
.
package t;
use Moo;
use Types::Standard qw(ArrayRef);
my @list = qw/foo bar baz/;
has list => (
is => 'rw',
isa => ArrayRef,
builder =>
default => sub [@list]
);
package u;
use Moo;
use Types::Standard qw(ArrayRef);
extends 't';
sub BUILD
my ($self) = @_;
push @$self->list(), qw/apple banana/;
return $self;
package main;
use Data::Printer;
my $u = u->new();
p $u->list();
my $t = t->new();
p $t->list();
While I'm at it, using BUILD to modify an attribute is possible but not necessarily the best. You can use something like a lazy attribute with a builder method, then overload that method in the subclass, ala
package t;
use Moo;
use Types::Standard qw(ArrayRef);
my @list = qw/foo bar baz/;
has list => (
is => 'rw',
isa => ArrayRef,
builder => '_build_list',
lazy => 1,
);
sub _build_list
my $self = shift;
return [@list];
package u;
use Moo;
extends 't';
sub _build_list
my $self = shift;
my $list = $self->SUPER::_build_list();
push @$list, qw/apple banana/;
return $list;
package main;
use Data::Printer;
my $u = u->new();
p $u->list();
my $t = t->new();
p $t->list();
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55047234%2fmoo-object-extends-order%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Since you mutate the array in question, you don't want a reference to the array that you use as the default @list
, you want to take a shallow copy [@list]
.
package t;
use Moo;
use Types::Standard qw(ArrayRef);
my @list = qw/foo bar baz/;
has list => (
is => 'rw',
isa => ArrayRef,
builder =>
default => sub [@list]
);
package u;
use Moo;
use Types::Standard qw(ArrayRef);
extends 't';
sub BUILD
my ($self) = @_;
push @$self->list(), qw/apple banana/;
return $self;
package main;
use Data::Printer;
my $u = u->new();
p $u->list();
my $t = t->new();
p $t->list();
While I'm at it, using BUILD to modify an attribute is possible but not necessarily the best. You can use something like a lazy attribute with a builder method, then overload that method in the subclass, ala
package t;
use Moo;
use Types::Standard qw(ArrayRef);
my @list = qw/foo bar baz/;
has list => (
is => 'rw',
isa => ArrayRef,
builder => '_build_list',
lazy => 1,
);
sub _build_list
my $self = shift;
return [@list];
package u;
use Moo;
extends 't';
sub _build_list
my $self = shift;
my $list = $self->SUPER::_build_list();
push @$list, qw/apple banana/;
return $list;
package main;
use Data::Printer;
my $u = u->new();
p $u->list();
my $t = t->new();
p $t->list();
add a comment |
Since you mutate the array in question, you don't want a reference to the array that you use as the default @list
, you want to take a shallow copy [@list]
.
package t;
use Moo;
use Types::Standard qw(ArrayRef);
my @list = qw/foo bar baz/;
has list => (
is => 'rw',
isa => ArrayRef,
builder =>
default => sub [@list]
);
package u;
use Moo;
use Types::Standard qw(ArrayRef);
extends 't';
sub BUILD
my ($self) = @_;
push @$self->list(), qw/apple banana/;
return $self;
package main;
use Data::Printer;
my $u = u->new();
p $u->list();
my $t = t->new();
p $t->list();
While I'm at it, using BUILD to modify an attribute is possible but not necessarily the best. You can use something like a lazy attribute with a builder method, then overload that method in the subclass, ala
package t;
use Moo;
use Types::Standard qw(ArrayRef);
my @list = qw/foo bar baz/;
has list => (
is => 'rw',
isa => ArrayRef,
builder => '_build_list',
lazy => 1,
);
sub _build_list
my $self = shift;
return [@list];
package u;
use Moo;
extends 't';
sub _build_list
my $self = shift;
my $list = $self->SUPER::_build_list();
push @$list, qw/apple banana/;
return $list;
package main;
use Data::Printer;
my $u = u->new();
p $u->list();
my $t = t->new();
p $t->list();
add a comment |
Since you mutate the array in question, you don't want a reference to the array that you use as the default @list
, you want to take a shallow copy [@list]
.
package t;
use Moo;
use Types::Standard qw(ArrayRef);
my @list = qw/foo bar baz/;
has list => (
is => 'rw',
isa => ArrayRef,
builder =>
default => sub [@list]
);
package u;
use Moo;
use Types::Standard qw(ArrayRef);
extends 't';
sub BUILD
my ($self) = @_;
push @$self->list(), qw/apple banana/;
return $self;
package main;
use Data::Printer;
my $u = u->new();
p $u->list();
my $t = t->new();
p $t->list();
While I'm at it, using BUILD to modify an attribute is possible but not necessarily the best. You can use something like a lazy attribute with a builder method, then overload that method in the subclass, ala
package t;
use Moo;
use Types::Standard qw(ArrayRef);
my @list = qw/foo bar baz/;
has list => (
is => 'rw',
isa => ArrayRef,
builder => '_build_list',
lazy => 1,
);
sub _build_list
my $self = shift;
return [@list];
package u;
use Moo;
extends 't';
sub _build_list
my $self = shift;
my $list = $self->SUPER::_build_list();
push @$list, qw/apple banana/;
return $list;
package main;
use Data::Printer;
my $u = u->new();
p $u->list();
my $t = t->new();
p $t->list();
Since you mutate the array in question, you don't want a reference to the array that you use as the default @list
, you want to take a shallow copy [@list]
.
package t;
use Moo;
use Types::Standard qw(ArrayRef);
my @list = qw/foo bar baz/;
has list => (
is => 'rw',
isa => ArrayRef,
builder =>
default => sub [@list]
);
package u;
use Moo;
use Types::Standard qw(ArrayRef);
extends 't';
sub BUILD
my ($self) = @_;
push @$self->list(), qw/apple banana/;
return $self;
package main;
use Data::Printer;
my $u = u->new();
p $u->list();
my $t = t->new();
p $t->list();
While I'm at it, using BUILD to modify an attribute is possible but not necessarily the best. You can use something like a lazy attribute with a builder method, then overload that method in the subclass, ala
package t;
use Moo;
use Types::Standard qw(ArrayRef);
my @list = qw/foo bar baz/;
has list => (
is => 'rw',
isa => ArrayRef,
builder => '_build_list',
lazy => 1,
);
sub _build_list
my $self = shift;
return [@list];
package u;
use Moo;
extends 't';
sub _build_list
my $self = shift;
my $list = $self->SUPER::_build_list();
push @$list, qw/apple banana/;
return $list;
package main;
use Data::Printer;
my $u = u->new();
p $u->list();
my $t = t->new();
p $t->list();
edited Mar 7 at 15:43
answered Mar 7 at 15:38
Joel BergerJoel Berger
18.1k44095
18.1k44095
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55047234%2fmoo-object-extends-order%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown