I presume he was reacting to:
if (@ARGV) { if (@ARGV == 1) { Download($ARGV[0]); EXIT(0, 'SUCCESS. Argument was received from command line.'); } else { PrintUsage(); EXIT(-1, 'Argument missing.'); } } else { Download(GetArgs()); EXIT(0, 'SUCCESS. Argument was received from stdin.'); }
That can be rewritten as:
if (@ARGV) { if (@ARGV == 1) { Download($ARGV[0]); EXIT(0, 'SUCCESS. Argument was received from command line.'); } PrintUsage(); EXIT(-1, 'Argument missing.'); } Download(GetArgs()); EXIT(0, 'SUCCESS. Argument was received from stdin.');
which reduces the number of lines of code and makes the logic clearer. Even better would be:
use strict; use warnings; if (!@ARGV) { Download(GetArgs()); EXIT(0, 'SUCCESS. Argument was received from stdin.'); } if (@ARGV == 1) { Download($ARGV[0]); EXIT(0, 'SUCCESS. Argument was received from command line.'); } PrintUsage(); EXIT(-1, 'Argument missing.');
which avoids nested code paths altogether. Wherever the code path can't continue out of a previous block there is no need for an else. That avoids nesting following code and generally makes the logic easier to follow.
In reply to Re: else after a return, exit, croak, die
by GrandFather
in thread else after a return, exit, croak, die
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |