Liam has asked for the wisdom of the Perl Monks concerning the following question:

Ok so i've written my first script, it asks for the users name, location etc.
#!/usr/local/bin/perl print "\nWhat is your name?"; $username = <STDIN>; print "\nWhere do you live?"; $userlocation = <STDIN>; print "\nHow old are you?"; $userage = <STDIN>; $userage2 = $userage++; print "\nAre you a boy or a girl?"; $usersex = <STDIN>; if ($usersex eq "boy") { $userboy = "he"; }elsif ($usersex eq "girl") { $userboy = "she"; } print "$username is $userage2 years old and lives in $userlocation" ." In a year $userboy will be $userage $username is a $usersex";
Problem is $userboy should equal he or she, but it shows up as nothing! how do i fix it?

Replies are listed 'Best First'.
RE: My first Script
by vroom (His Eminence) on Feb 08, 2000 at 01:51 UTC
    Your problem is that when you read in from the keyboard you get the "\n" (newline) as well. You can chomp that variable in yourcase $usersex, like chomp $usersex and it should work.
      To clarify what vroom has said, change your lines so that where you are asking for input you have (for example):chomp ($usersex = <STDIN>);This will also prevent the output looking like this:
      setantae is 23 years old and lives in Wales In a year will be 24 setantae is a boy
      $userboy will still show up as nothing unless the user enters exactly boy or girl. You may also want to check for more common user input, such as b, g, Boy, BOY, etc.
      setantae@eidosnet.co.uk|setantae|http://www.setantae.uklinux.net
RE: My first Script
by Anonymous Monk on Feb 08, 2000 at 04:14 UTC
    The statement $usersex = <STDIN> stores the input typed by the user (that's good) and also includes the newline when the user hit enter (that's bad). The final string probably looks like "boy\n". This caused the statement .. if ($usersex eq "boy") .. to return false. Take a look at this more correct example: print "\nAre you a boy or a girl?"; $usersex = <STDIN>; chomp $usersex; # remove trailing newline if ($usersex eq "boy") { $userboy = "he"; } elsif ($usersex eq "girl") { $userboy = "she"; } print "$userboy is a $usersex"; Also, you may consider using the uc function to make your string comparison case-insensitive. :-) Hope this helps...
Re: My first Script
by stephen (Priest) on Feb 09, 2000 at 04:46 UTC
    To the above answers, I wanted to add this advice: use a subroutine to ask the questions and do the chomping. If you have a subroutine that asks the question, then returns the result, the code will be cleaner. So, summarizing the above advice:
    #!/usr/local/bin/perl $username = ask_user("What is your name?"); $userlocation = ask_user("Where do you live?"); $userage = ask_user("How old are you?"); $userage2 = $userage++; $usersex = ask_user("Are you a boy or a girl?"); $userboy = get_gender($usersex); print "$username is $userage years old and lives in $userlocation" ." In a year $userboy will be $userage2 $username is a $usersex"; sub ask_user { my ($question) = @_; print "\n$question "; my $answer = <STDIN>; chomp($answer); $answer; } sub get_gender { my ($usersex_in) = @_; my $usersex = lc($usersex_in); if ($usersex eq "boy") { return "he"; } elsif ($usersex eq "girl") { return "she"; } else { return "it"; } }

    Hope that was helpful.