Useful errors for Moose and MooseX::Declare
Moose is very lovely, but sometimes simple typos can cause hair-raisingly exciting long stacktraces with, from my point of view, zero useful content.
So, are there any tools to interpret this exploding into something helpful?
In particular for classes using plain Moose, Moose+MooseX::Method::Signatures, and MooseX::Declare.
The tools only need to be helpful while developing to catch those typo or thinko problems that make things just not work.
=========================
Following suggestion below, I'm using this not-quite-a-module-yet which is reducing my headaches a little, more ideas welcome, though:
package MooseX::QuietCarping;
# Not actually a Moose thing, but helpful for Moose.
# calm Moose-internal stacktraces down a little
use Carp;
my %retain = ();
sub import {
my $class = shift;
$retain{$_}++ for @_;
}
CHECK {
for (sort keys %INC) {
s{\.pm$}{开发者_如何学C};
s{[/\\]}{::}g; # CROSS PLATFORM MY ARSE
next if $retain{$_};
$Carp::Internal{$_}++ if /^(?:Class::MOP|Moose|MooseX)\b/
}
%retain = (); # don't need this no more
}
1;
One way I experimented with some time ago is putting Moose related classes into %Carp::Internal
hash, something like this:
$Carp::Internal{$_}++ for qw{
Class::MOP
Class::MOP::Attribute
Class::MOP::Class
...
};
Such classes will be skipped in stack trace, making it more compact and emphasizing your own code.
You can find them by traversing %INC
variable:
package Dummy;
use Moose;
use MooseX::Declare;
# use ....;
for (sort keys %INC) {
s{\.pm$}{};
s{/}{::}g;
print "$_\n" if /^(Class::MOP|Moose|MooseX)\b/;
}
I seem to recall seeing a PerlMonks post by stvn a week or two ago saying that they're working on improving the Moose error messages. I don't think there's anything currently available to clean the up, though.
Method::Signatures::Modifiers
is a package which hopes to fix some of the problems of MooseX::Method::Signatures
. Simply use
it to overload.
use MooseX::Declare;
use Method::Signatures::Modifiers;
class Foo
{
method bar (Int $thing) {
# this method is declared with Method::Signatures instead of MooseX::Method::Signatures
}
}
精彩评论