Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Re: Re: Re: Getopt::Long

by hotshot (Prior)
on Dec 16, 2002 at 09:25 UTC ( [id://220145]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Re: Getopt::Long
in thread Getopt::Long

nice approach although not generic since each time I'll add a new command to my shell, I'll have to update %commands carfully - check first letters of each command. quite a mess if I have lots of commands, and anyhow, my list of commands is generated according to the menu I'm in (my shell is quite big and menus oriented). the available commands list is generated on each TAB key pressing (for tab completion), so I'll have to think maybe how to generelize your suggestion. I think it's suits smaller and less complicated systems then I got here, but thanks for the effort.

Hotshot

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Getopt::Long
by ibanix (Hermit) on Dec 16, 2002 at 20:27 UTC
    If you keep doing it the way you mentioned above, your code is going to look like:
    if ($command eq "add") { .... } elseif ($command eq "view") { .... } elseif ($command eq "something") { .... } elseif ( ... ) { .... } elseif ( ... ) { .... } elseif ( ... ) { .... } elseif ( ... ) { .... } elseif ( ... ) { .... } elseif ( ... ) { .... . . . . }
    Do you really think a large list of if/else statements is the most efficent way to do things?

    If you have multiple command menus, then do this:
    %menus = ( 'first' => { 'add' => '&add', 'view' => '&view', 'a' => '\$menus{first}{add}', 'v' => '\$menus{first}{view}', }, 'second' => { 'delete' => '&delete', 'print' => '&print', }, );
    Then you can invoke a command for a menu:
    $menus{first}{$_} if exists $menus{first}{$_}; or print "Command not found.\n" if not exists $menus{first}{$_};
    Also, this scheme has the advantage of letting you have commands that point to other commands in different menus. Furthermore, if you ever update a command, you only need edit it's sub.

    I would be using this method for large and complicated systems, despite what you say above. Your method strikes me as, and I apoligize for using the word, a kludge.

    Regards,
    ibanix

    $ echo '$0 & $0 &' > foo; chmod a+x foo; foo;

Log In?
Username:
Password:

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

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

    No recent polls found