in reply to Re: Conditional question
in thread Conditional question

  • 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
  • Replies are listed 'Best First'.
    Re^3: Conditional question
    by Corion (Patriarch) on Oct 19, 2007 at 14:04 UTC
      #!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.

          But Perl does not:

          C:\Projekte>type tmp.pl #!/usr/bin/perl -w use strict; my $foo; print $foo __END__ C:\Projekte>perl tmp.pl Use of uninitialized value in print at tmp.pl line 5.