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?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.