Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Minor query

by spudzeppelin (Pilgrim)
on Jun 24, 2000 at 01:00 UTC ( [id://19668]=note: print w/replies, xml ) Need Help??


in reply to Minor querry!!

Or, supposing you want to allow decimals:

FOO: $a = <STDIN>; chomp($a); $a =~ tr/0-9.//cd; if (length($a) < 1) { print "Please enter a number\n"; goto FOO; }

Spud Zeppelin * spud@spudzeppelin.com

Replies are listed 'Best First'.
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.
      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; } }

      All these loops are so long! Labels and status variables and chomping, oh my!

      How about this:

      { 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+$/;
        And turnstep said:
        But my personal favorite would be simply:
        print "Number?\n" while <STDIN> !~ /^\d+$/;
        Of course, you'll not have the number you read, unless you look at $& (expensive) or put \d+ inside parens, and then look at $1.

        -- Randal L. Schwartz, Perl hacker

      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

        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.
        
        While they may be identical algorithmically, I find the while() loop preferable for two reasons.
        1. It is more readable (sorry spud. It is. Deal with it.)
        2. 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://19668]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-03-29 10:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found