Hello monks,

I am using Moose for several last projects of mine and I am very happy with it. Only thing is sometimes quite confusing - its error messages. Given this example:

package Cls; use Moose; has an_array => ( traits => ['Array'], isa => 'ArrayRef[Str]', is => 'rw', # !!! forgotten default to empty array handles => { list_array => 'elements', add_item => 'push', }, ); package main; func(); sub func { my $c = Cls->new(); $c->add_item('String'); }

Running code above, the error message is this:

Can't use an undefined value as an ARRAY reference at C:/Perl/site/lib/Moose/Meta/Attribute/Native/MethodProvider/Array.pm line 130.

I am looking for a way how to assume that Moose is always correct and the error is in my code (on add_item in func sub) for the sake of reporting.

I made an attempt to achieve this, but I am not very happy with the solution:

package MooseX::Silent; use Carp qw(confess); $Carp::Internal{$_}++ for qw{ MooseX::Silent MooseX::Method::Signatures::Meta::Method Moose::Meta::Attribute::Native::MethodProvider::Hash Moose::Meta::Attribute::Native::MethodProvider::Array Moose::Meta::Attribute::Native::Trait }; $SIG{'__DIE__'} = sub { my $msg = shift; for($msg) { s/^.*Internal Validation Error is: //; s/MooseX::Types::Structured:://g; s/ at .*? line \d*\.//; chomp; } confess "$msg"; }; 1;

When this module is used in the Cls class, the error message becomes:

Can't use an undefined value as an ARRAY reference at tst.pl line 22 main::func() called at tst.pl line 18

On the other hand this is pretty global and enumeration of packages is also quite inconvenient.

Is there better way to achieve similar results? Any idea or suggestion welcome.

-- thanks, Roman


In reply to Moose warnings by bobr

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.