in reply to defined class variable with Moose
G'day Hosen1989,
I suspect you may be mixing (and consequently, confusing) this type of Moose syntax:
has attribute => ( default => sub { 0 }, );
with normal Perl syntax for creating key-value pairs:
{ default => 0, }
sub { 0 } is a code reference.
For your simpler cases, like
description => sub{0}
you could just use
description => 0
However, regardless of the complexity of the code within sub { ... }, what you really want is the return value when that code is executed. For that, use
sub { ... }->()
Consider these three one-liners:
$ perl -wE 'my $x = { y => 0 }; say $x->{y}' 0
$ perl -wE 'my $x = { y => sub { 0 } }; say $x->{y}' CODE(0x7f8c34026a60)
$ perl -wE 'my $x = { y => sub { 0 }->() }; say $x->{y}' 0
-- Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: defined class variable with Moose
by Hosen1989 (Scribe) on Jun 26, 2015 at 20:02 UTC | |
by kcott (Archbishop) on Jun 26, 2015 at 21:17 UTC |