The problem one often faces is where at first sight standards and/or best practices conflict. In this case, there are at least three potentially conflicting factors that may need to be considered: 1) avoiding excessive nested blocks in terms of length and depth, 2) having a single point of return or exit from a routine or subroutine and 3) avoiding goto.
Example:
sub fred {
# meeting all three requirements:-
my ($t, $u, $v, $w, $x, $y) = @_;
somelogger( 5, "fred", "start of fred");
my $ok = method1($t);
$ok &&= method2($u);
my $pid = open my $ph, "/somewhere/program $v |";
my $output = '';
$ok &&= method3( <$ph>, \$output );
close $ph;
waitpid $ph,0;
# and so on for functionality using $w, $x and $y (in worst case s
+cenario)
# if goto were used to jump to the exit point the
# label would have been here.
if ($ok) {
somelogger( 5, "fred", "fred completed successfully");
}
else {
somelogger( 5, "fred", "error during fred");
}
return $ok;
}
The point is that if goto were avoided using ifs, there would be one nesting level per error condition. Using &&= enables the code to cascade (without extra nested blocks) to the end whenever an error occurs. But many might consider the &&= too techy for the average programmer. Any thoughts, alternatives?
-
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.