RE: Re: Minor query
by Shendal (Hermit) on Jun 24, 2000 at 01:17 UTC
|
While your example using tr/// is interesting, the use of the g-word (goto) is a bad idea. Instead, use a loop:
while (!$stop) {
$a = <STDIN>;
chomp($a);
$a =~ tr/0-9.//cd;
if (length($a) < 1) {
print "Please enter a number\n";
$stop = 1;
}
}
Of course, I think the other examples, especially plaid's suggestion are less obfuscated.
| [reply] [Watch: Dir/Any] [d/l] |
|
a slightly nicer fusion of your two methods is the following:
PROMPT: {
chomp($a = <STDIN>);
$a =~ tr/0-9.//cd;
unless (length($a))
{
print "Please enter a number\n";
redo PROMPT;
}
}
| [reply] [Watch: Dir/Any] [d/l] |
|
{
print "Number?\n";
redo unless <STDIN> =~ /^\d+$/;
}
Or, to save the value (which can be useful :)
{
print "Number?\n";
redo unless <STDIN> =~ /^(\d+)$/ and $number=$1;
}
But my personal favorite would be simply:
print "Number?\n" while <STDIN> !~ /^\d+$/;
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
| [reply] [Watch: Dir/Any] [d/l] |
|
|
I beg to differ. "Never, never, never use goto!" is one
of those programming mantras which are taught to beginners
because at that level, the temptation is to use it where it
is not appropriate. However, as a developer becomes more
experienced, the usefulness, clarity, and simplicity of an
unencumbered "jump over there" instruction make it more
valuable when used sparingly and appropriately. And in this
case, an exception handler of a sort that basically says "go
back three lines and try it again," the use is very appropriate.
While we are drifing dangerously offtopic, I am going to ask you
to reconsider the two snippets above, and tell me which is actually
more readable -- the one that obfuscates a goto with a while loop, or
the one that explicity uses the goto when the goto is EXACTLY the
instruction being used in both, algorithmically.
Spud Zeppelin * spud@spudzeppelin.com
| [reply] [Watch: Dir/Any] |
|
as St. Larry once said,
If I allowed "next $label" then I'd also have to allow "goto $label",
and I don't think you really want that... :-)
-- Larry Wall
|
Of course both were in fact added, but you can clearly see the concern expressed over "goto."
The worst thing about goto is that those of us who spent our teenage years writting spaghetti in BASIC that consisted of 30% GOTO will be so frightened and appalled at the sight of it, that we will run screaming in the other direction. Proabably not the response you want your code to get...
Paris Sinclair | 4a75737420416e6f74686572
pariss@efn.org | 205065726c204861636b6572
I wear my Geek Code on my finger.
| [reply] [Watch: Dir/Any] |
|
While they may be identical algorithmically, I find the while() loop preferable for two reasons.
- It is more readable (sorry spud. It is. Deal with it.)
- It is more maintainable (A clearly marked block is always preferable.)
But of course, I think goto has its place, and should occaisonally be used. If you think about it, at the assembly level, goto is the only looping mechanism available. And of course, TIMTOWTDI.
| [reply] [Watch: Dir/Any] |