in reply to Moose arrayref addition: noob
G'day jorba,
Either the code you posted is not what you ran or you haven't shown us the complete output.
I created "pm_1201774_moose_demo.pm", which I'm going to modify in stages, as I show the output when testing, and make fixes.
To start with, here's a very minimal version to show an error you should have seen:
package Demo; use Moose; has test => (is => 'rw', isa => 'ArrayRef'); sub check { $self->test(()); }
As Moose automatically turns on the strict pragma:
$ perl -c pm_1201774_moose_demo.pm Global symbol "$self" requires explicit package name (did you forget t +o declare "my $self"?) at pm_1201774_moose_demo.pm line 8. pm_1201774_moose_demo.pm had compilation errors.
Changing &check to declare $self:
sub check { my ($self) = @_; $self->test(()); }
That problem is now fixed:
$ perl -c pm_1201774_moose_demo.pm pm_1201774_moose_demo.pm syntax OK
Now I add code to emulate your error.
package Demo; use Moose; has test => (is => 'rw', isa => 'ArrayRef'); sub check { my ($self) = @_; $self->test(()); $self->test()->[0] = 'X'; } package main; Demo::->new->check;
Note the syntax is still fine; your error occurs at runtime.
$ perl -c pm_1201774_moose_demo.pm pm_1201774_moose_demo.pm syntax OK $ perl pm_1201774_moose_demo.pm Can't use an undefined value as an ARRAY reference at pm_1201774_moose +_demo.pm line 12.
In "$self->test(());", your argument is an empty list which will flatten to nothing; i.e. it's the same as "$self->test();". This is an accessor; I believe you want a mutator (I'm guessing this would be to reinitialise that attribute).
In order for that method to act as a mutator, you need to pass a value of an appropriate type (in this case: isa => 'ArrayRef'), such as:
$self->test([]);
Now the syntax is still OK and the runtime error no longer occurs.
$ perl -c pm_1201774_moose_demo.pm pm_1201774_moose_demo.pm syntax OK $ perl pm_1201774_moose_demo.pm $
Compare that with your problem two days ago:
... Fields ... isa => 'HashRef' ... $self->Fields({});
And, if that Fields() method is intended to be the same as the one in your current code:
$Cnt = scalar $self->Fields;
I suspect you're going to have a problem with that also. Here's a hint:
$ perl -E 'my $x = {a=>1,b=>2}; say scalar $x; say scalar keys %$x' HASH(0x7ffe120040b0) 2
And looking at the two lines after that:
$self->FieldCount = $Cnt; $self->Changed = -1;
You're going to get the same errors that you had in your last question. Frankly, they seem unrelated to your current issue and I don't why you posted them; however, if these haven't changed:
... FieldCount ... isa => 'Num' ... ... Changed ... isa => 'Boolean' ...
You'll end up with repeats of:
... Can't modify non-lvalue subroutine call at ...
Furthermore, using "-1" as a boolean value is likely to be highly confusing (and, therefore, error-prone). It's also probably just wrong, as a "SELECT" statement is a read-only operation. I'd expect the value of "Changed" to be false, but:
$ perl -E 'my $x = -1; say $x ? "true" : "false"' true
— Ken
|
|---|