Perl Tutorial
Fundamentals
Input and Output
Control Flow
Arrays and Lists
Hash
Scalars
Strings
Object Oriented Programming in Perl
Subroutines
Regular Expressions
File Handling
Context Sensitivity
CGI Programming
Misc
In Perl, standard input (often abbreviated as STDIN
) is a predefined input stream that provides a way to read data from the console (or from a piped source). This tutorial covers how to use STDIN
in Perl to obtain input from the user or another program.
The simplest way to read from STDIN
is using the <>
operator. This reads one line from STDIN
.
print "Please enter your name: "; my $name = <STDIN>; print "Hello, $name";
When this script is run, it will wait for the user to enter their name and then greet them.
After reading from STDIN
, it's common to have a newline character (\n
) at the end of the input. You can remove it using the chomp
function:
print "Please enter your age: "; my $age = <STDIN>; chomp($age); print "In ten years, you will be ", $age + 10, " years old.\n";
You can read multiple lines of input from STDIN
using a loop:
print "Enter several lines (Ctrl-D to end):\n"; my @lines = <STDIN>; print "\nYou entered:\n", @lines;
The above will continue reading input until you signal an end-of-file with Ctrl-D (or Ctrl-Z on Windows).
To read a single character from STDIN
, you can use the getc
function:
print "Press a key: "; my $char = getc(STDIN); print "You pressed: $char\n";
STDIN
in List ContextIn a list context, reading from STDIN
will read all remaining lines:
print "Enter several lines:\n"; my @all_lines = <STDIN>; print "You entered ", scalar @all_lines, " lines.\n";
You can also use STDIN
to read input that is piped from another program. For example:
echo -e "apple\nbanana\ncherry" | perl script.pl
If script.pl
contains:
while (my $fruit = <STDIN>) { print "You piped in: $fruit"; }
The script will print each fruit that was echoed into it.
For security reasons, if you're reading a password or other sensitive information, it's a good idea not to display the typed characters. The Term::ReadKey
module, which isn't a core module but is commonly used, can assist with this:
use Term::ReadKey; print "Enter your password: "; ReadMode('noecho'); # Don't display characters my $password = ReadLine(0); # Read from STDIN ReadMode(0); # Reset input mode chomp($password); print "\nThank you.\n";
Using STDIN
in Perl provides a way to interactively receive input from the user or get data from other programs. Combined with functions like chomp
, getc
, and modules like Term::ReadKey
, Perl offers robust ways to read and handle input effectively.
Reading user input with STDIN in Perl:
print "Enter your name: "; my $name = <STDIN>; chomp($name); print "Hello, $name!\n";
Getting input from the command line in Perl:
my $arg1 = $ARGV[0]; my $arg2 = $ARGV[1]; print "Arguments: $arg1, $arg2\n";
Perl STDIN and chomp function:
chomp
to remove the newline character from user input.print "Enter a number: "; my $number = <STDIN>; chomp($number);
Handling user input with STDIN in Perl scripts:
print "Enter your age: "; my $age = <STDIN>; chomp($age); if ($age =~ /^\d+$/) { print "Valid age: $age\n"; } else { print "Invalid input. Please enter a number.\n"; }
Input validation using STDIN in Perl:
print "Enter a positive number: "; my $input = <STDIN>; chomp($input); if ($input =~ /^\d+$/ && $input > 0) { print "Valid input: $input\n"; } else { print "Invalid input. Please enter a positive number.\n"; }
Interactive console input in Perl:
print "What is your favorite color? "; my $color = <STDIN>; chomp($color); print "You like $color! Great choice.\n";
Reading multiple lines from STDIN in Perl:
print "Enter a poem (Ctrl-D to end):\n"; my @poem_lines = <STDIN>; chomp(@poem_lines);
STDIN as an array in Perl:
print "Enter three names (one per line):\n"; my @names = <STDIN>; chomp(@names); print "You entered: @names\n";