basic perl conditional script not working -
i beginner perl , have been messing around trying create little scripts. i'm not sure wrong here falls through else
every time if nothing input satisfies if
or elsif
conditions. because eq
wrong operator? or there else wrong in code? thanks!
#!/usr/bin/perl use strict; use warnings; print "what name?\n"; $name = readline stdin; print "hello $name how today?\n"; $feeling = readline stdin; if ($feeling eq "happy") { print "that's good!\n"; } elsif ($feeling eq "good") { print "okay!\n"; } else { print "interesting\n"; }
use chomp($feeling);
#!/usr/bin/perl use strict; use warnings; print "what name?\n"; $name = readline stdin; chomp($name); print "hello $name how today?\n"; $feeling = readline stdin; chomp($feeling); if ($feeling eq "happy") { print "that's good!\n"; } elsif ($feeling eq "good") { print "okay!\n"; } else { print "interesting\n"; }
readline stdin captures every character typed along last enter hit \n
, if type "happy"
, hit enter $feeling
accepted "happy\n"
notice \n
because enter hit remove last \n
newline character use chomp removes trailing string
Comments
Post a Comment