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


In reply to Re: Moose arrayref addition: noob by kcott
in thread Moose arrayref addition: noob by jorba

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.