Sorry, I can see why my example is confusing. Here's a slightly better one, more true to the production code:
#!/usr/bin/perl
use strict;
use warnings;
use Scalar::Util qw(looks_like_number);
my @times = (2, 4, 3, 1, 3.5, 2, 5);
select((select(STDOUT), $|=1)[0]);
TIME: for my $time_to_wait (@times) {
print "Time to wait is: $time_to_wait\n";
local $@;
eval {
# Skip even numbers.
die '__NOT_FATAL__' if !($time_to_wait % 2);
verify_number($time_to_wait);
# Skip numbers less than 3.
die '__NOT_FATAL__' if ($time_to_wait < 3);
verify_time($time_to_wait);
1;
}
or do {
if($@ && $@ =~ /__NOT_FATAL__/) {
next TIME;
}
print "Something went boom! ($@)\n";
};
print "Sleeping for $time_to_wait seconds ... ";
sleep $time_to_wait;
print "Done.\n";
}
exit 0;
sub verify_number {
my ($arg) = @_;
die "We died!" if !defined $arg;
looks_like_number($arg) or die "We died!";
return;
}
sub verify_time {
my ($arg) = @_;
die "We died!" if !defined $arg;
# Floats not allowed.
int($arg) == ($arg / 1) or die "We died!";
return;
}
Now imagine several more "verify" type calls within that same eval. Granted, it could be re-written to use a bunch more separate eval calls thus potentially allowing next calls outside/between them but in production those function calls are all very related and would die in similar ways. So I feel it comes down to what's more simple, cleaner? I think I might give the undermentioned Syntax::Keyword::Try package a try (swidt) since it does seem to support exiting via "next" calls.
-s1m0n-
-
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.
|