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

Ohh venerable monks, I am trying to write a simple program that allows dialogue between the user and program. Below is the code, I was wondering if anyone could clue me into why this isnt working properly. Thanks -yellowbeard
#!/usr/bin/perl $temp = <STDIN>; if ($temp eq "yo") { print "whats up"; } elsif ($temp eq "not much") { print "cool"; } else ($temp eq "<STDIN>") { print "HUH?"; }

Title edit by tye

Replies are listed 'Best First'.
Re: Code question?
by jasonk (Parson) on Mar 11, 2003 at 23:53 UTC

    Because $temp contains a carriage-return, although I'm not sure what you are trying to do with that last one. (Also, your print statements do NOT contain a carriage return, and so they won't print quite the way you expect.)

    chomp($temp = <STDIN>);
      Thanks muchly jasonk
Re: Code question?
by zengargoyle (Deacon) on Mar 11, 2003 at 23:55 UTC
    #!/usr/bin/perl use strict; while(my $temp = <STDIN>) { chomp $temp; # removes the "\n" from the end. if ($temp eq "yo") { print "whats up\n"; } elsif ($temp eq "not much") { print "cool\n"; } }

    you need the 'chomp' to remove the "\n" from the line you read in. it also looks like you'll want to loop to get the next line and the next line ... from the user.

Re: Code question?
by mowgli (Friar) on Mar 12, 2003 at 00:07 UTC

    It might also be worth noting that there is no condition after an else; the purpose of that one is, after all, to catch everything that hasn't been handled yet, so to speak.

    Also, are you sure that $temp eq "<STDIN>" (notice the quotes) is what you want, anyhow? It looks rather suspicious to me.

    --
    mowgli

Re: Code question?
by Cody Pendant (Prior) on Mar 12, 2003 at 00:44 UTC
    I think your next step should be to create a hash of all possible inputs and responses.

    %call_and_response = ( 'yo', 'what\'s up', 'not much', 'cool', 'how about that local sports team?', 'they suck', );

    Then you can just do

    if($call_and_response{$call}){ print $call_and_response{$call}; }else{ print "huh?"; }
    or something like that.
    --
    “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
    M-J D
Re: Code question?
by nite_man (Deacon) on Mar 12, 2003 at 08:05 UTC
    You can use a following construction which look like switch operator:
    #!/usr/bin/perl -w use strict; SWITCH: for(<STDIN>) { /^[Yy]o$/ && { print "whats up"; last; }; /^[Nn]ot much$/ && { print "cool"; last; }; print "HUH?" }
    
    ====>
    Mice were crying and stinging but went on nibbling the cactus ... 
                                                  ( The facts of life )
                                                                  <====