Perl STDIN

In Perl programming, we can get input from standard console using <STDIN>. It stands for Standard Input. It can be abbreviated by <>. So,

my $name = ;

is equivalent to:

my $name = <>;

Perl Input from User using <STDIN>

Example#1

Let's see an example where we are getting data from the user using Standard Input <STDIN>.

Type in:

snippet
use 5.010;
use strict;
use warnings;
say "Enter your Name:";
my $name = ;
say "Welcome $name at rookienerd.";

Here, $name is a scalar variable. Variables are declared using my keyword. After running this script, it will ask for your name. Type your name in the console and press ENTER.

Output
Enter your Name: John Welcome John at rookienerd.

As you can see in the above output, a new line is added after printing the name. To overcome this problem, add the chomp function with $name as shown below.

Example #2
snippet
use 5.010;
use strict;
use warnings;
say "Enter your Name:";
my $name = ;
chomp $name;
print "Welcome $name at rookienerd.\n";
Output
Enter your Name: John Welcome John at rookienerd.
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +