How to Run Perl Scripts on the Linux Command Line

Share this:

Call them ancient, but Perl scripts are still a great way to automate various sysadmin tasks. If you are a beginner and wondering how to run Perl scripts in a Linux terminal, just use this command:

perl script.pl
How to Run Perl Scripts on the Linux Command Line

This will work even if the script file does not have permission to execute.

You can also run it as a bash script:

./script.pl

But for this to work, the file must have execute permission, and the script must begin with a hashbang:

#!/usr/bin/perl

If the syntax is correct and Perl is installed on your system, you should be able to execute Perl scripts easily.

Read also: How to split a Gzip file on Linux

How do you know if Perl is installed on your system?

Use this command and if you see the version information, then everything is fine.

perl --version
perl script

If your system gives you an error like “Perl command not found”, use your system’s package manager to install it.

There are other ways to use Perl on Linux.

You can write a Perl script right in the terminal and run it right away without even saving it.

Let’s look at this in a little more detail.

Executing Perl Scripts from Files

This is the recommended way to create and execute Perl scripts.

You save scripts and can use them in an IDE like Eclipse if the script is complex or is part of a large Perl project.

See also  Pokemon Go: Hoopa Unbound Elite Raid Best Counters

The Perl file doesn’t have to have any extension, but the “.pl” extension is good practice.

Create a new file with the touch command like this:

touch my_file.pl

The newly created file will be empty, I will use the Nano editor to modify the empty file.

nano my_file.pl

My example script looks like this:

#!/usr/bin/perl
print "Enter your name: ";
$name=<STDIN>;
print "Hello, ${name} ... you will soon be a Perl addict!";
print "\n";

Save and exit the Nano editor with Ctrl+X.

You don’t need to give the script permission to execute.

Use it like this:

perl my_file.pl

You can also give the script permission to execute and run it like this:

./my_file.pl

But if your goal is simply to check Perl syntax, you don’t need to create and save scripts.

Running a Single Line Perl Command on Linux

This is a quick method for a quick Perl syntax check.

To execute a Perl command, you just need to follow the given syntax:

perl -e <Perl code>

You just need to pay attention to the quotes in the bash shell.

perl -e 'print "Hello world!\n";'

You can pass multiple lines with multiple -e options.

However, there is a better way to write multiple lines of Perl code without saving them to a file.

Executing Perl commands without saving them to a file

This method is a bit similar to what you saw above, but it’s less complicated.

Run a Perl prompt with the given command:

perl

Then write the desired script.

Once you are done writing, you should press CTRL+D to show the End of File (EOF) prompt.

See also  How to try Spotify Premium for free: all about your trial period

The Perl script will be executed immediately after pressing Ctrl+D.

For example, I want to run the following script:

print "Enter your name: ";
$name=<STDIN>;
print "Hello, ${name} ... you will soon be a Perl addict!";
print "\n";

This simple script should take input from the user (name in this case) and return something like this:

Perl

Conclusion

As you can see, running Perl scripts on Linux is not much different than running bash shell scripts.

Perl is a powerful scripting language, and while Python is gaining popularity, experienced sysadmins still use it for complex scripting.

Checkout also: 11 reasons Vivaldi is the best browser you’re not using

Share this:

Leave a Comment