Perl | STDIN in Scalar and List Context
Last Updated :
18 Feb, 2023
STDIN in Perl is used to take input from the keyboard unless its work has been redefined by the user.
Syntax: <STDIN> or <>
STDIN in Scalar Context
In order to take input from the keyboard or operator is used in Perl. This operator reads a line entered through the keyboard along with the newline character corresponding to the ENTER we press after input. Example:
Perl
# Asking user for Input
print "What is your age?\n";
# Getting an age from the user
$age = <STDIN>;
# Removes new line from the input
chomp $age;
# Printing the value entered by user
print "Your age is ", $age;
Output:
So $age contains the input given by the user as well as the new line character. In order to remove the new line, chomp function is used which removes “\n” from the end of the string.
Taking user Input without using <STDIN> in Scalar Context -
We can take user Input without using <STDIN>.
Perl
#!/usr/bin/perl
print("Please enter your age : ");
$age = <>;
print("Your age is $age");
output -
STDIN in List Context
When STDIN is used with list context, it takes multiple values as an input from the keyboard. Press ENTER to indicate individual elements in the list. In order to indicate the ending of inputs, press Ctrl-D in Linux systems whereas Ctrl-Z in Windows system. The example below shows the use of STDIN in list context. Example:
Perl
# Get a city name from the user
print "Enter the cities you have visited last year... ";
print "<Ctrl>-D to Terminate \n";
@city = <STDIN>;
# Removes new line appended at
# the end of every input
chomp @city;
# Print the city names
print "\nCities visited by you are: \n@city ";
Output:
Here is how the above program works: Step 1: Get List input from the user separated by ENTER. Step 2: When Ctrl-D is pressed it indicates the ending of inputs, so, Perl assigns everything to the @city array. Step 3: Use chomp function to remove new line from all the inputs. Step 4: Printing the city names given as in input.
Taking user Input without <STDIN> in List Context -
We can also take user input in List Context using <> and to mark the end of taking input we will press CTRL+Z for Windows and CTRL+D for Linux.
Perl
#!/usr/bin/perl
print("Enter the cities you have visited last year : ");
@cities = <>;
chomp @cities;
print("You have visited the following cities @cities");
Output -
Similar Reads
Perl | Hash in Scalar and List Context Prerequisite: Perl Hash Hash in Perl is a set of key/value pairs. Perl provides us the flexibility to assign the hash to a List type and a Scalar type, known as LIST Context and SCALAR Context respectively. Hash in LIST Context The assignment of a hash to a list type in Perl is accomplished by makin
5 min read
Perl | Scalar Context Sensitivity Introduction: In Perl, function calls, terms, and statements have inconsistent explications which rely upon its Context. There are two crucial Contexts in Perl, namely List Context and Scalar Context. In a list context, Perl gives the list of elements. But in a scalar context, it returns the number
5 min read
Perl | List Context Sensitivity Introduction In Perl, function calls, terms, and statements have inconsistent explications which rely upon its Context. There are two crucial Contexts in Perl, namely List Context and Scalar Context. In a list context, Perl gives the list of elements. But in a scalar context, it returns the number o
4 min read
Perl | Comparing Scalars Prerequisite: Scalars in Perl Perl has two types of comparison operator sets. Just like other mathematical operators, instead of performing operations, these operators compare scalars. There are two types of sets of Perl comparison operators. One is for numeric scalar values and one is for string sc
6 min read
Perl List and its Types Introduction to Lists A list is a collection of scalar values. We can access the elements of a list using indexes. Index starts with 0 (0th index refers to the first element of the list). We use parenthesis and comma operators to construct a list. In Perl, scalar variables start with a $ symbol wher
4 min read