zachlipton has asked for the wisdom of the Perl Monks concerning the following question:
While goto may be harmful and all, I have a need to use it in this case and it makes logical sense for the program flow. I'm trying to avoid hackind it with a loop, but my goto statement isn't working. Here's a code sample:
ASK_DATABASE:
ask('databasetype','Database type?','mysql');
my $found = undef;
foreach $cur (@databases) {
if (getConf('databasetype') eq $cur) {
$found = 1;
last;
}
}
unless ($found) {
output "Sorry, invalid option\n\n";
goto ASK_DATABASE;
}
This runs fine as long as I enter a valid database type (as defined in @databases), but if I enter something invalid, instead of goto'ing back to the call to ask() again, I get this:
Can't find label ASK_DATABASE at Conf/NewConfiguration.cm line 49, <STDIN> line 7.
If anyone has a recomendation on how best to fix this, it would be great.
•Re: On goto
by merlyn (Sage) on Mar 01, 2003 at 01:55 UTC
|
Trivially eliminating the goto, I'd go with:
{
ask('databasetype','Database type?','mysql');
my $found; # aside: I hate " = undef"
foreach $cur (@databases) {
if (getConf('databasetype') eq $cur) {
$found = 1;
last;
}
}
unless ($found) {
output "Sorry, invalid option\n\n";
redo;
}
}
Next, I hate the artificial $found variable, so
I'd merely move that into a subroutine:
{
ask('databasetype','Database type?','mysql');
unless (is_databasetype_valid()) {
output "Sorry, invalid option\n\n";
redo;
}
}
sub is_databasetype_valid {
foreach my $cur (@databases) {
if (getConf('databasetype') eq $cur) {
return 1;
}
}
return 0;
}
That'd probably be enough, but that call to getConf seems to be perhaps invariant in that loop, so I'd pull it out to call the subroutine only once during the scan of @databases.
But hopefully, you get the idea now.
-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply. | [reply] [Watch: Dir/Any] [d/l] [select] |
|
{
ask('databasetype','Database type?','mysql');
output("Sorry, invalid option\n\n"), redo
if not grep getConf('databasetype') eq $_, @databases;
}
Unless @databases is so large that you need the short-circuiting behaviour of the foreach version.
Makeshifts last the longest. | [reply] [Watch: Dir/Any] [d/l] |
|
That's fine if you're golfing.
I tend to be a bit more explicit in code that I'm leaving around for someone else to read. That last statement has about six semantic steps that partially nest, partially flow right to left, and partially flow left to right, without the help of denotational punctuation to help me "change gears".
It's also riddled with booby traps for the maintainer. The parens on output are mandatory, or else the redo would trigger before the function is called. If the expression for the test gets more complicated, or wants to be called only once, there's a bit of gyrating to do.
I figure anything that takes me more than a dozen seconds to parse needs to be broken up for production code. My personal rule.
See, if you were just going for compact, I'd slide that
even further to:
output("Sorry, invalid option\n\n")
until
ask('database','Database type?', 'mysql'),
grep getConf('databasetype') eq $_, @databases;
This gets rid of the naked block entirely.
But again, I would not use this in production code. This is cute to impress your friends, especially your Lisp-hacking friends. But it really aggravates the maintainer who has to try to grok that at 3am when the production web site crashes.
-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply. | [reply] [Watch: Dir/Any] [d/l] |
|
|
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
| [reply] [Watch: Dir/Any] |
|
|
|
Re: On goto
by Jenda (Abbot) on Mar 01, 2003 at 19:22 UTC
|
while (ask('databasetype','Database type?','mysql')) {
last
if (grep {getConf('databasetype') eq $_} @databases);
output "Sorry, invalid option\n\n";
}
And the ask() would return true if the user replied to the question and false if he accepted the default (no need to validate the default I guess) or pressed CTRL+D/CTRL+Z (end of input).
Jenda | [reply] [Watch: Dir/Any] [d/l] |
Re: On goto
by hgolan30 (Initiate) on Mar 01, 2003 at 10:46 UTC
|
if u want the script will not die just put it in eval
like
my $answer;
eval{$answer=getConf('databasetype');};
if($@){
# this is an error
}
| [reply] [Watch: Dir/Any] [d/l] |
|
|