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, context is crucial for understanding how various functions and operations behave. Two of the main contexts in Perl are scalar context and list context. The behavior of STDIN
is influenced by these contexts. Let's explore this in more detail.
In Perl, STDIN
is a global filehandle used to get input from the standard input, typically the keyboard or redirected file input. How you use STDIN
can differ depending on whether you're in scalar or list context.
In scalar context, reading from STDIN
returns a single line of input:
print "Enter your name: "; my $name = <STDIN>; chomp $name; # Remove the trailing newline print "Hello, $name!\n";
In the above, <STDIN>
reads just one line of input from the user.
In list context, reading from STDIN
returns all the remaining lines from the input until the end-of-file (EOF) is reached:
print "Enter multiple lines (Ctrl-D to end):\n"; my @lines = <STDIN>; print "\nYou entered:\n", @lines;
Here, Perl will keep reading lines until you signal EOF. On UNIX-like systems, EOF is signaled by pressing Ctrl-D
, while on Windows, it's Ctrl-Z
.
The diamond operator (<>
) is similar to <STDIN>
, but it has some special behavior. By default, it reads from STDIN
, but if command-line arguments are provided, it treats them as filenames and reads from those files:
# Reads from files listed in @ARGV, or STDIN if no files provided while (my $line = <>) { print $line; }
When reading from STDIN
(or any filehandle), Perl retains the newline character (\n
). To remove it, use the chomp
function:
my $input = <STDIN>; chomp $input; # Removes trailing newline
When dealing with list context, you can chomp all lines at once:
my @inputs = <STDIN>; chomp @inputs;
Remember that context is vital in Perl. A function or operation can behave differently based on its surrounding context:
my $single = <STDIN>; # Reads one line (scalar context) my @many = <STDIN>; # Reads all lines until EOF (list context)
STDIN
is the standard input filehandle in Perl.<STDIN>
reads one line from the input.<STDIN>
reads all lines until EOF.<>
) reads from STDIN
or files provided in @ARGV
.chomp
your input to remove trailing newlines.Reading from STDIN in scalar context in Perl:
print "Enter your name: "; my $name = <STDIN>; chomp $name; print "Hello, $name!\n";
Using <STDIN>
in list context in Perl:
print "Enter three lines:\n"; my @lines = <STDIN>; print "You entered:\n@lines";
Scalar vs. list context with STDIN in Perl:
print "Enter a number: "; my $num_scalar = <STDIN>; # Scalar context chomp $num_scalar; print "Enter three lines:\n"; my @lines_list = <STDIN>; # List context print "Number (Scalar): $num_scalar\n"; print "Lines (List): @lines_list";
Perl reading multiple lines from STDIN:
print "Enter lines (Ctrl-D to end input):\n"; my @input_lines = <STDIN>; chomp @input_lines; foreach my $line (@input_lines) { print "Processed: $line\n"; }
Processing user input from STDIN in Perl:
print "Enter your age: "; my $age = <STDIN>; chomp $age; if ($age =~ /^\d+$/) { print "Valid age: $age\n"; } else { print "Invalid input for age.\n"; }
STDIN as an array in Perl:
print "Enter lines (Ctrl-D to end input):\n"; my @lines = <STDIN>; chomp @lines; foreach my $line (@lines) { print "Processed: $line\n"; }
Iterating over STDIN in Perl list context:
print "Enter lines (Ctrl-D to end input):\n"; while (<STDIN>) { chomp; print "Processed: $_\n"; }
Chomping newlines from input in Perl STDIN:
chomp
when reading from STDIN.print "Enter a string: "; my $input = <STDIN>; chomp $input; print "Processed: $input\n";