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

CLI and problems

by hotshot (Prior)
on Aug 17, 2003 at 16:00 UTC ( [id://284420]=perlquestion: print w/replies, xml ) Need Help??

hotshot has asked for the wisdom of the Perl Monks concerning the following question:

Hello all !

I'm writing a command line interface, and ran into the following problem: Almost all commands in the CLI run directly or indirectly (function that call function that call function...) a function named getParam(). My problem is that if the getParam() fails I want to display an error message and return immediately to the prompt (the main loop of the CLI) without needing to return false from it to the calling funtion, check it's return value and so on until the main loop, it's quite ugly this way, for example:
sub getParam { if ($someCondition) { return 0 } ... return 1; } sub execCommand { ... if (! &func1()) { return 0; } ... return 1; } sub func1 { ... if (! &getParram()) { return 0; } .. return 1; } # CLI main loop while ($input = $term->readline("\n$prompt > %n", $TRUE)) { &execCommand($input); }
As you can see, in each function that calls &getParam() (inderectly) I have to check the return value all the way up to the main loop. Is there a way to avoid all the checks and return somehow immediatelly from getParam() to the main loop (after the call to execCommand())?

Thanks

Hotshot

Replies are listed 'Best First'.
Re: CLI and problems
by esh (Pilgrim) on Aug 17, 2003 at 17:25 UTC

    Besides the other, more drastic changes, you could simplify your existing algorithm by using an idiom like the following. Replace:

    if (! &func1()) { return 0; }
    with:
    func1() or return 0;

    This reduces the lines of code, puts the function call up front where it commands attention, and pushes the error code and return value to a quiet after thought.

    Notice that I also dropped the "&" in the function call. (Yes, I remember Perl 4).

    Oh, if you want to return if the function returns true, replace "or" with "and" as in:

    func2() and return 0;

    -- Eric Hammond

Re: CLI and problems
by dreadpiratepeter (Priest) on Aug 17, 2003 at 16:15 UTC
    You should look into exception handling. You would throw and exception at the point of the error (in getparam), and then catch it in a catch block defined in your main loop.
    You could fake it with eval/die, but I would recommend looking at the Error module on CPAN.


    -pete
    "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."
Re: CLI and problems
by mattriff (Chaplain) on Aug 17, 2003 at 16:11 UTC
    Does getparam() have function specific arguments? If not, call it once in execCommand(), and have it pass the results to the subroutines being called.

    That way, if it fails you can print your error and return to the while loop immediately.

    For that matter, you could have getParam() itself print warnings on error, put it in the while loop, and not even call execCommand() if it fails.

    On a side-note, if 0 is always a failure, you can replace your if statements with:

    &getParam() or return 0;

    That's nicer to look at IMO, but TIMTOWDI and YMMV. :)

    - Matt Riffle
      Sr. Systems Programmer, pair Networks, Inc.
      (although, I'm speaking only for myself; code is untested unless otherwise stated)

Re: CLI and problems
by benn (Vicar) on Aug 17, 2003 at 16:12 UTC
    One way would be to wrap your "execCommand()" call in an 'eval' - then getParam() could die, and you simply check $@ for success / failure.

    Cheers, Ben.

Re: CLI and problems
by Anonymous Monk on Aug 17, 2003 at 22:58 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (None)
    As of 2024-04-19 00:04 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found