How to Work With User Input in Perl

How to Work With User Input in Perl


Perl is a language that is useful for scripting, file processing, and other forms of development. The command line can be used to gather a user’s input and collected by Perl.


A Perl Interpreter (should be native on Linux, Active State Perl is one distribution for Windows), and a command line interface (CLI) such as command prompt/cmd.exe on Windows or a shell on Linux.


Set up your text file (with file extension .pl) as follows, starting from the first line:

use strict;
use warnings;
my $data;

The first line indicates where the Perl interpreter is located. This may be irrelevant on Windows, but whether it is included or not, the location will be used on Linux. The next lines signal what mode to run in. Strict mode with warnings will throw errors and display warnings if present. The last line is declaring the location where the user input data will be stored.

Following this, let’s collect user input:

print “Please enter some data, then press Enter.\n”;
$data = ;

The above lines first print a message to the screen, followed by a newline (\n). Then, the data is collected by the end user who is given control after the prompt. Following this, the chomp() function will remove the \n newline at the end of the data, whether it is there or not. $data then contains the data for further usage, after being collected by .