Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: crafting a decent CLI

by Abigail (Deacon)
on Jul 12, 2001 at 15:44 UTC ( [id://95979]=note: print w/replies, xml ) Need Help??


in reply to (tye)Re: crafting a decent CLI
in thread crafting a decent CLI

That way your code actually looks like "loop reading commands until you get a non-empty one and loop doing that until a 'quit' is requested". Not, "here is a block that I may or may not be using as a loop (please parse the rest of the block if you want to know), read a command, goto the top of the nearest surrounding loop (or naked block but not a conditional or do block) if the command is empty, otherwise go to the continue block (if there is one) or the top of the nearest surrounding loopish block, if a 'quit' was not requested". (:
And for that, you use a "here's a do, please parse till the end to see whether I want to include a file, I want to turn a block into an expression or I want to use a looping construct"?

At least with a bare block you know you have a looping construct. With do you can have 3 totally different things. On top of that, several of your examples nest dos. I can understand that some people find bare blocks confusing - I indicated you could use a while(1) in front of it - but I fail to see that nested do constructs are easier than a bare block.

I would think that the major "problem" with my bare block is that the guard is at the end - but your do { } until suffers from the same problem.

Here's how I read my block:

  • print prompt.
  • read line and chomp it.
  • try again if there are no non-blanks.
  • execute command and repeat if we don't quit.
More or less the same as you would explain things in English. And isn't that what's a major design principle Larry uses for Perl? (Note that my next should have been a redo).

-- Abigail

Replies are listed 'Best First'.
(tye)Re2: crafting a decent CLI
by tye (Sage) on Jul 12, 2001 at 23:53 UTC

    Actually, you don't have to parse the rest of the do block to figure out what is going on. Once you see ";\ndo {\n" you know (from the ";" and newlines) that it isn't a (sensible) conversion of a block to an expression and (from the "{") that it isn't "do file".

    There was a smiley after the paragraph you quote. The point of that was to illustrate how hard the loop control shortcuts can be to parse. Your example was quite small so figuring out where each control change branched to was not difficult. I still prefer "do {" because it makes most people think "loop" much faster and because the branches stand out much more. Hiding a branch with something small that doesn't change indentation (like redo or goto) means that the structure of the code is not nearly as visually discernable.

    So I avoid naked blocks, redo, next, and last because they are easy to abuse and require more visual energy to parse. That doesn't mean I never use them, of course (though I've never found a use for naked block as a loop). For example, redo, next, and last are important tools for allowing you to use Perl's most natural looping construct (for(@list)) in more situations rather than resorting to a more error-prone alternative.

    As a few other examples of this have shown recently, pulling the "naked block plus redo" out for very simple problems can lead to producing much more complex code than is required because it doesn't put much pressure on you to figure out the structure of the code since they give you such abitrary control of the flow.

    And it certainly isn't a tool I would ever suggest to a person already having problems writing fairly simple code in a well-structured manner.

            - tye (but my friends call me "Tye")
      Actually, you don't have to parse the rest of the do block to figure out what is going on. Once you see ";\ndo {\n" you know (from the ";" and newlines) that it isn't a (sensible) conversion of a block to an expression and (from the "{") that it isn't "do file".
      I do not quite agree with that. You do know the expression modifiers, don't you? EXPR if EXPR and EXPR for EXPR etc are quite common in Perl. Every now and then you want to do two or more things in the first expression. EXPR, EXPR if EXPR sometimes works, but sometimes it won't. So you use do {EXPR; EXPR} if EXPR. That do is followed by a '{' and typically preceeded by a ";\n". And hopefully you aren't giving special meaning to the newline. ;-)

      -- Abigail

        That is what if( EXPR ) { EXPR; EXPR } is for. So, no, I don't consider that a sensible use of do.

                - tye (but my friends call me "Tye")

Log In?
Username:
Password:

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

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

    No recent polls found