Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Stumped UPDATED!?!?!

by jcassie420 (Initiate)
on Feb 11, 2008 at 04:23 UTC ( [id://667314]=perlquestion: print w/replies, xml ) Need Help??

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

Sorry for not being completely clear before but i just wanted to know if there was a better way to do it than a whole lot if if statements. But i still thank you for taking the time to reply. Origonal post: Dear Perl Monks, I am new to perl programming and currently writing my first useful program but i am having a problem trying to make a "command prompt" I cant seem to find a better way to do it than this:
sub more { print "more()\n" } sub fallover { print "fallover()\n" } print "Welcome to johns fun house\n"; print " : "; my $command=<>; chomp $command; if ($command eq "fun") { more; } if ($command eq "laugh") { fallover; }
Also, i would like to offer more than just two options.I have searched many tutorials searching for an awnser and havn't found one so your help is greatly appreciated...

Replies are listed 'Best First'.
Re: New and Stumped
by kyle (Abbot) on Feb 11, 2008 at 04:38 UTC

    jcassie420,

    Welcome to the Monastery. Please have a look at Writeup Formatting Tips. It appears your entire question is in code tags, but only the code should be.

    In answer to your question, see here:

    sub more { print "more()\n" } sub fallover { print "fallover()\n" } print "Welcome to johns fun house\n"; print " : "; my $command=<>; chomp $command; if ($command eq "fun") { more; } if ($command eq "laugh") { fallover; }

    The first important change is to us "eq" when comparing strings. The "==" comparison is only for numbers. See perlop for details.

    The other important change is to chomp the input after you get it. Otherwise, $command will have a trailing line terminator.

    As an aside, if you haven't looked into them already, I recommend you use strict and warnings with everything you write. You may want to go without them later, but for now they'll probably save you more headaches than they cause.

Re: Stumped UPDATED!?!?!
by ysth (Canon) on Feb 11, 2008 at 05:43 UTC
      For example,
      use strict; use warnings; $| = 1; # auto flush stdout sub more { print "more()\n" } sub fallover { print "fallover()\n" } sub leave { print "Bye!\n"; exit; } my %cmdhash = ( fun => \&more, laugh => \&fallover, exit => \&leave, ); print "Welcome to johns fun house\n"; while (1) { print " : "; my $command=<>; chomp $command; if (my $func = $cmdhash{$command}) { $func->(); } else { print "I'm afraid I don't know how to \"$command\"\n"; } }
        $| = 1; # auto flush stdout

        Why?

Re: New and Stumped
by Anonymous Monk on Feb 11, 2008 at 04:28 UTC
    use eq not ==

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-16 17:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found