bobr has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Moose warnings
by moritz (Cardinal) on May 06, 2010 at 14:05 UTC | |
by Corion (Patriarch) on May 06, 2010 at 14:13 UTC | |
by stvn (Monsignor) on May 06, 2010 at 19:42 UTC | |
by bobr (Monk) on May 06, 2010 at 15:04 UTC | |
|
Re: Moose warnings
by stvn (Monsignor) on May 06, 2010 at 19:26 UTC | |
by bobr (Monk) on May 07, 2010 at 09:21 UTC |