white-fox has asked for the wisdom of the Perl Monks concerning the following question:

hello monks.....
i realy newbie to perl....
i try to write a program that this program have a input command line...
now...what is the best way to write a program for giving a command from my command line program ?
for example my command line program like this ">>"
now user for example type ">>do_command1" or ">>help" how can i recognize what user type...and do that command... must use "if"?
my program maybe have 7 or 10 command...it means i must use if for 7 period?for example...:
$input=<stdin> if ($input == "help") { statment1; }

if u can introduce a program like my program..it maybe usefull

Replies are listed 'Best First'.
Re: problem with input command
by borisz (Canon) on Mar 27, 2005 at 12:15 UTC
    There is nothing wrong with your aproach. Just use eq to compare strings == is for numbers. You may also checkout chomp.
    For more comfortable editing look at Term::Readline.
    Boris
Re: problem with input command
by muba (Priest) on Mar 27, 2005 at 12:29 UTC
    I have written a sample program for you, using a hash. Maybe you won't truly understand it right now, but you will as you get more experienced with Perl. I'd suggest you read some books, tutorials and the Perl manual.

    Click here to see my sample program for you :

    Try entering the commands 'hello', 'world' and 'hacker'.
    Note that this program won't support arguments. That would be too complicated to show in a simple sample program like this.

    Anyway, welcome to Perl Monks, and good luck learning Perl! As you will discover, it's an amazing language.

    Update: there was an error in the code. I fixed it. I also corrected a spelling error.




    "2b"||!"2b";$$_="the question"
    Besides that, my code is untested unless stated otherwise.
    One more: please review the article about regular expressions (do's and don'ts) I'm working on.
Re: problem with input command
by gam3 (Curate) on Mar 27, 2005 at 12:31 UTC
    Using a perl type case statment (sometimes called a hash) can work well for this type of problem.
    sub help { print(@_); print(" This is help?\n"); } sub list { print("This is list\n"); } our $commands = { 'help' => \&help, 'list' => \&list, }; while (<STDIN>) { chomp; if ((my $name) = ($_ =~ /^(\w+)/)) { if (defined $commands->{$name}) { $commands->{$name}->($_); } else { print "I Don't understand $name\n"; } } }
    -- gam3
    A picture is worth a thousand words, but takes 200K.

        As it was posted only 2 minutes after yours, there's a good chance that it was submitted without gam3 having seen your post. (I know I have tendency to do more than one thing at once, so the posting time of my messages may have no direct relation to when I read the thread, especially for longer posts, or those that contain code samples that I want to verify beforehand).

        I'm guessing that if there was a specific reason that someone would post a varient of someone else's code, they would make reference to it, and explain why they've decided to take the alternative approach.

Re: problem with input command
by cog (Parson) on Mar 27, 2005 at 13:15 UTC
    For instance:

    print "Insert your command:"; $input = <STDIN>; if ($input eq "help") { # do something } elsif ($input eq "other command") { # do something else }

    Of course, if your commands are allowed to receive parameters,

    print "Insert your command:"; $input = <STDIN>; if ($input =~ /^help\s+(.*)/) { # do something } elsif ($input =~ /^other command\s+(.*)/) { # do something else }

    The parameters (the full string following the original command and any spaces after it) will be store in the special variable $1

    You can even have a final condition for the case in which the inserted command was not valid:

    else { print "'$input' is not a valid command. Type help for a full list of + commands.\n"; }