Mr.Clean has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Guys. I am new here but I have a quick question that I haven't been able to find an answer for. When you declare a variable as STDIN, it will assign the inputed information to the variable you declared(as I'm sure you already know this.). And there are loads of tutorials out there on that kind of stuff, but I can't find any tutorials that actually teach you how to do it with a text input! This is how I would expect it to work but it obviously doesn't:
#!usr/bin/perl print "type hw to print hello world script!"; $cmd = <stdin>; if ( cmd = hw ) { print "Hello, world"; } else { Print "sorry I didn't understand the command."; }

Replies are listed 'Best First'.
Re: Conditional question
by marto (Cardinal) on Oct 19, 2007 at 11:35 UTC
    "I can't find any tutorials that actually teach you how to do it with a text input!"

    Did you look in the tutorials section of this site? If you had done you would have found Perl Babysteps 1: Your First Simple Script. When posting here please post error messages, saying things "obviously" don't work is not ideal. Check out How do I post a question effectively? and the PerlMonks FAQ if you have not already done so.

    It looks to me that you could benefit from reading some documentation (see comments in code below) and some basic tutorials
    #!usr/bin/perl use strict; # perldoc strict use warnings; # perldoc warnings print "type hw to print hello world script!\n:> "; my $cmd = <STDIN>; chomp($cmd); # perldoc -f chomp if ( $cmd eq "hw" ){ # does $cmd contain hw? print "Hello, world"; }else{ print "sorry I didn't understand the command."; }
    Hope this helps

    Update: changed stdin to STDIN thanks to Corion's keen eye :)

    Martin
Re: Conditional question
by Corion (Patriarch) on Oct 19, 2007 at 11:27 UTC

    Maybe you care telling us how it "obviously doesn't [work]" ?

    It's not that I couldn't name at least ten eight things wrong with your code, but I expect people to do some work of their own, and running perl -c (see perlrun) is the least.

    • use strict
    • use warnings
    • use my before first variable use
    • it is STDIN not stdin
    • chomp input to remove new line
    • use $ in front of variables
    • use eq to compare, = is assignment not comparison
    • Put quotes around strings

      Are those your 8?


      ___________
      Eric Hodges
        #!usr/bin/perl -w #1 use strict; #2 print "type hw to print hello world script!\n"; #3, newline(s) missing my $cmd = <STDIN>; #4 STDIN if ( $cmd eq "hw" ) #5,6,7 $cmd, "=" should be "==" or "eq", "hw" must + be quoted { print "Hello, world\n"; #3, newline(s) missing } else { print "sorry I didn't understand the command.\n"; #8,3 "Print" -> "pri +nt", newline(s) missing }

        I didn't count the missing my as an error, that could have been intended as a global variable instead of a lexical with global scope. I missed the chomp, and I had the \n after the print statements so the buffers get flushed. I'm not sure if Perl autoflushes buffers when alternating between printing to STDOUT and reading from STDIN.

Re: Conditional question
by almut (Canon) on Oct 19, 2007 at 11:29 UTC

    Details matter :)  Try this:

    #!/usr/bin/perl print "type hw to print hello world script!"; my $cmd = <STDIN>; chomp $cmd; # get rid of trailing newline if ( $cmd eq 'hw' ) { print "Hello, world"; } else { print "sorry I didn't understand the command."; }
Re: Conditional question
by perlfan (Parson) on Oct 19, 2007 at 13:38 UTC
    if ( cmd = hw ) is wrong on many levels. A single "=" is an assignment, not a comparison; and unless you've created a constant using use constant var => "value";, then you can't use bare words.
      Sorry. I was in a hurry while typing this and I tried using quotes on "hw" before but i don't think it worked. I'm new to PERL so try not to be to hard on me please(this is the first language I've even attempted to learn). So, yeah. Note: this is not a reply to just perlfan's post. It is to all posts.
Re: Conditional question
by apl (Monsignor) on Oct 19, 2007 at 12:25 UTC
    This is how I would expect it to work but it obviously doesn't:

    It obviously doesn't because of a glaring typo. (HINT: It has nothing to do with STDIN.)

    Have you tried using the debugger, or judious print statements? Another HINT: After testing, what command wasn't understood?
Re: Conditional question
by andreas1234567 (Vicar) on Oct 19, 2007 at 11:46 UTC
Re: Conditional question
by ww (Archbishop) on Oct 22, 2007 at 14:13 UTC
    Given your comment that this is your first shot at programming, here's something that's either a nit or a crucial distinction:
    "type hw to print hello world script!";

    As constructed (and, even as corrected by marto), your script will NOT print a script; it will print only the greeting you placed in quotes.

    How to actually print a script, self-referentially or otherwise, is something you'll find here with the search or supersearch tools..and a carefully constructed query.