in reply to Conditional question

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.

Replies are listed 'Best First'.
Re^2: Conditional question
by eric256 (Parson) on Oct 19, 2007 at 13:55 UTC

  • 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.

        I now use warnings; instead of perl -w since Windows seems to ignore the she-bang line.


        I humbly seek wisdom.