Updated April 4, 2023
Introduction to Perl STDIN
Perl STDIN is used to get input from standard console by using STDIN in perl. It is also called as standard input. STDIN is abbreviated as <> in perl, we can abbreviate it as <STDIN> or it is equivalent to a <>. We have declared the scalar variable that are setting equal to <> or <STDIN>, we can also set the variable equal to whatever we have type on the command prompt. STDIN is very important and useful in perl to get standard input from console or take input from keyboard and any other input device.
Syntax:
<STDIN>
my $variable_name = <STDIN>;
Below is the parameter description for the syntax as follows:
- STDIN: STDIN is stands for standard input which we have used to take input from the user or to take from the keyboard or any input device. The STDIN will read the line entered from the keyboard along with the new line character and special character corresponding with the enter key which we have press after input.
- Variable name: We have used any variable name to define STDIN in perl. We have used variable name to declare STDIN in perl. Variable name parameter is very important and useful in perl to declare value of STDIN variable. We have defined any name to the variable that we have used with STDIN in perl.
How STDIN works in Perl?
- The STDIN will read the line entered from the keyboard along with the new line character and special character corresponding with the enter key which we have pressed after input.
- STDIN stands for standard input which we have used to take input from the user or to take from the keyboard or any input device.
- We can use it with the list context in perl. While using it with list of context it will take multiple value as input from the user.
- After pressing enter it will identify that it would be individual elements in the list. We can press ctrl+D in Linux systems and ctrl+Z in windows system to indicate the end of the inputs.
- It is abbreviated as <> in perl, we can abbreviate it as <STDIN> or it is equivalent to a <>.
- It is very important and useful in perl to get standard input from console or take input from keyboard and any other input device.
- It is used to get input from standard console by using it in perl. It is also called as standard input.
- We have declared scalar variable that are setting equal to <> or <STDIN>. We can also set the variable equal to whatever we have typed on the command prompt.
- We can use it with the scalar context in perl. Scalar context is an operator in perl which have used with STDIN in perl.
- Scalar context operator will read the line which was entered from the keyboard along with the new line character in perl.
- Scalar context and list context operator is very useful and important. It is used to take input from the user by using STDIN.
- We can declare it with variable name, also we can use any variable name to declare and initialize input.
- We have defined any name of the variable that we have used with STDIN in perl. Variable name parameter is very important and useful in perl to declare value of STDIN variable.
Examples
Given below are the examples mentioned:
Example #1
STDIN in perl.
In below example we are getting data from the user using standard input.
Code:
use 5.030;
use strict; ## Use strict and warnings
use warnings;
say "Enter Name:";
my $name_user = <STDIN>; ## Declare variable name as name_user using STDIN.
say "Welcome $name_user in perl";
Output:
In above example we have taken name as input from the user. After taking input it will display the end line of welcome in perl.
Example #2
STDIN using scalar context.
Below example shows a scalar context in perl. We are using scalar context operator. Scalar context operator will read the line which was entered from the keyboard along with the new line character in perl.
Code:
print "Enter age of user\n";
# Getting age from the user by using age_user variable.
$age_user = <STDIN>;
# Removes new line from the input by using chomp in perl.
chomp $age;
print "Age is ", $age_user;
Output:
We have taken input as age from the user. We have removed new line character by using chomp. Chomp function removes the new line character from the code.
Example #3
STDIN using list context.
Below example shows STDIN with a list context in perl. We are using list context operator.
Code:
# Get name from the user
print "Enter name";
print "<Ctrl>-D to terminate\n"; ## Use ctrl-D to terminate using STDIN in perl.
@user_name = <STDIN>;
# Removes new line appended using chomp in perl.
chomp @user_name;
# Print the name of the user.
print "\nUser name is: \n@user_name ";
Output:
Advantages
- STDIN is used to take input from the user or any input device.
- It is very useful and important in perl to take input from the keyboard or from the user.
- We can get input from the standard console by using STDIN in perl.
- It is abbreviated as <>, it can be equivalent to the <STDIN>.
- We can take input of scalar and list context operator by using it.
Conclusion
STDIN stands for standard input and abbreviated the name as <STDIN>. The STDIN will read the line entered from the keyboard along with the new line character and special character corresponding with the enter key which we have pressed after input. It is very useful in perl.
Recommended Articles
We hope that this EDUCBA information on “Perl STDIN” was beneficial to you. You can view EDUCBA’s recommended articles for more information.