In fact, after reading eyepopslikeamosquito’s response I'm reminded of this kind of approach. It’s not very hard, it can DWIW a little with overload, and it can be very useful/robust compared to matching for content against error strings–
use 5.014; # Safer eval+$@ handling.
use strictures;
package HURR_DERR_ERR {
use Moo;
use Devel::StackTrace;
use overload '""' => sub { +shift->as_string };
has code =>
is => "ro",
default => sub { "[uncoded]" };
has message =>
is => "ro",
required => 1;
has stacktrace =>
is => "ro",
default => sub { Devel::StackTrace->new->as_string };
sub as_string {
my $self = shift;
join ": ", $self->code, $self->message;
}
};
for ( 1 .. 100 )
{
eval {
die HURR_DERR_ERR->new( message => "Unlucky number encountered
+: $_" )
if /\b4\b/; # You'll have to localize if you don't want Ch
+inese values.
};
if ( $@ )
{
# If you want it -> say $@->stacktrace;
die "Got an error -> $@";
}
else
{
say;
}
}
1
2
3
Got an error -> [uncoded]: Unlucky number encountered: 4 at example.pl
+ line 33.
Update: changed “script name” in error output. Update #2: changed version per suggestion.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|