PERL - Chomp
Let's take another look at our agencolor.pl script. This script asked
for two user inputs and returned an unformatted string and our
variables as a result on the last line.
agencolor.pl:
#! usr/bin/perl
print "How old are you?";
$age = <>;
print "What is your favorite color?";
$color = <>;
print "You are $age, and your favorite color is $color.";
Here we have asked two questions, stored the user's input into two
separate variables and printed out the results all in just a few lines
of code. Be sure to take note of the formatting here. The user is
required to strike the "enter" key resulting in a line break. We can
remove the line break rather easily with the
chomp() function. This function simply removes any erroneous line breaks and spacing from the end of our string.
agencolor2.pl:
#! usr/bin/perl
print "How old are you?";
chomp ($age = <>);
print "What is your favorite color?";
chomp ($color = <>);
print "You are $age, and your favorite color is $color.";
Age 'N' Color 2:
Chomp should be used whenever input is acquired from your users.
0 comments:
Post a Comment