in reply to Re: why dosn't my code work?
in thread why dosn't my code work?
chomp($name... female;} is not some sort of form definition that only gets activated when you issue the print "\n\n\n" or the <> at the bottom of your sample code. I can see how someone used to working with HTML might think that way, but this is procedural code.
That speculation about the OP's thinking seems a little far-fetched to me. I'd say the structure of the code looks like a rather straightforward attempt at something that's supposed to sequentially ask the user a couple of questions, and print out a summary at the end. Just like those typical exercises you find in many introductory textbooks on programming. Or like a simple interactive install script querying some parameters. I presume the OP is well aware of the procedural characteristic, and that <return> needs to be hit after every answer. And the print "\n\n\n"; was presumably just intended to create three lines of vertical space between the input and output section. (Only the OP can tell, though...)
The reasons the script 'doesn't work' is just a couple of newbie glitches like using == in place of eq, an \n-outside-of-string typo, or suboptimal things like using barewords instead of properly quoted strings. With those fixed, the script works fine as is.
A corrected version could look like this (which would also run under use strict):
use strict; print "What is your name?\n"; chomp(my $name = <>); print "Thank you $name. What is your age?\n"; chomp(my $age = <>); if ($age <= 18) { print "Are you a boy or girl?\n";} elsif ($age > 18) { print "Are you a man or woman?\n";} print "Type M or F.\n"; chomp(my $char = <>); my $gender = 'unknown'; if ($char eq 'm' || $char eq 'M'){ $gender = 'male';} elsif ($char eq 'f' || $char eq 'F'){ $gender = 'female';} print "\n\n\n"; print "Name: $name\n"; print "Age: $age\n"; print "Gender: $gender\n\n"; <>; __END__ What is your name? Almut Thank you Almut. What is your age? 17 :) Are you a boy or girl? Type M or F. f Name: Almut Age: 17 :) Gender: female
Due to Perl's convenient string-number conversions, it even accepts age input which is not strictly numeric (with use warnings, you'd get a warning, though).
Just two more remarks:
As the two inequality tests if ($age <= 18) and elsif ($age > 18) are complementary, the latter condition seems superfluous1. You might argue that it's ok for documentary purposes, but from a maintainability point of view, it's preferable to just use a plain if (...) { } else { }, because then you wouldn't need to worry about keeping the two conditions in sync, should the critical age ever need to be changed.
On the other hand, the code handling the gender answer might additionally want to provide some defined outcome (e.g. "unknown") in case the user enters something unexpected like 'h'.
___
1 this kinda reminds me of the following snippet of C++ code once posted on The Daily WTF ;)
bool is_true(bool val) { if (val == true) return true; else if (val == false) return false; else return !true && !false; }
(from memory)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: why dosn't my code work?
by ELISHEVA (Prior) on Apr 21, 2009 at 11:50 UTC | |
by Zen (Deacon) on Apr 21, 2009 at 14:05 UTC | |
by Anonymous Monk on Apr 22, 2009 at 04:04 UTC | |
by ELISHEVA (Prior) on Apr 22, 2009 at 04:38 UTC |