"Perl for Perl Newbies" - Part 1Input

5. Input

In order to receive a value from the user, perl supplies the <> operator. When entered, this operator reads a line from the command line, and returns it (along with the newline character).

Here's an example:

print "Please enter your name:\n";
$name = <>;
print "Hello, ", $name, "!\n";

Notice the chomp function which strips off the trailing newline character from the variable. You would usually want to use it, when getting input from the user.

Here's another example:

print "Please enter a string:";
$string = <>;
print "The string you entered contains ", length($string), " characters.\n";

Written by Shlomi Fish