×


How to create and run a Perl script in Debian 10 ?

Perl scripts are yet another powerful method of executing complex programs with a very minimal effort. The people who have a good know-how of programming in C and C++ get along with Perl very nicely because of its close resemblance to these languages. 

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform Perl related queries.

In this context, we shall look into how to create Perl script and run it in Debian 10.


Method of Creating a Perl Script in Debian 10?

For creating a simple Perl script in Debian 10, there are three main steps that you need to follow:


1. Checking the Perl Version:

You should assure that you have Perl installed on your Debian 10 system. You can do this simply by checking the version of Perl by executing the following command in your terminal:

perl --version

If Perl has already been installed on your system, then running the above-mentioned command will render the version of Perl installed on your Debian 10 system.


2. Ensuring the Presence of Perl Interpreter:

Since every scripting language requires an interpreter which can help in the execution of the programs created in that language, therefore, you also need to ensure that the Perl interpreter has been installed on your system or not. 

You can check this out by running the following command in your terminal:

which perl

Running this command will show you the path to the binary of Perl.


However, if you also want to check the path of the source file and the man pages of Perl along with the binary file for an added assurance, then you can run the following command in your terminal:

whereis perl

The output of the "whereis" command will display all the locations of Perl in the system.


3. Creating a Perl Script in Debian 10:

Once you have ensured the presence of Perl and its respective interpreter on your Debian 10 system, there comes the turn of creating a Perl script. A Perl script can conveniently be created by executing the following command in your terminal:

sudo nano MyFirstPerlScript.pl

Any other text editor can also be used to serve the same purpose instead of the nano editor, however, we have used this editor solely because it is the default text editor of the Linux systems. Moreover, you can have any name for your Perl script that must be followed by a .pl extension.


When you will run this command, an empty Perl script file will open with the nano editor. You just have to type the following script in your file. The first line of this script is “#!/bin/perl” which is there to explicitly state that the following script is a Perl script. Then we have simply printed a dummy message by making use of the “print” command. After typing in this Perl script in your file, you should save it and exit from the editor by pressing Ctrl+ X.


Methods of Running a Perl Script in Debian 10:

You can run a Perl script by using either of the two methods listed below:


1. Using the "chmod" Command to Set Execute Permissions:

For this method, you first have to set the execute permissions for your Perl script after which you can finally run it. The details of both these steps are listed below:


i. Setting the Execute Permissions:

You have to make the Perl script that you have just created, executable by running the following command in your terminal:

sudo chmod +x MyFirstPearlScript.pl

ii. Running the Perl Script:

After making your Perl script executable, you can run it by executing the following command in your terminal:

./MyFirstPerlScript.pl

Running this script will show you its output on the terminal.


2. Using the “perl” Command:

This method is an easier workaround for running a Perl script in Debian 10. It is so because it does not require you to set up any execute permissions rather you can simply run your Perl script straightaway.

You can run your Perl script by executing the following command in your terminal:

perl MyFirstPerlScript.pl

Running this script will show you its output on the terminal.


[Need urgent assistant with running packages on your Linux Server? We are available to help you. ]


Conclusion

This article will guide you on steps to create your first #Perl #script and execute it in Debian 10. Here you will learn the basic method of creating and running a simple Perl script. Once you manage to learn this, you can use Perl scripting for lots of other programmatically complex problems.

To run a Perl script:

1. Write and Run Your First Script. All you need to write Perl #programs is a text editor.

2. Write Your Script. Create a new text file and type the following exactly as shown: #!usr/bin/perl.

3. Run Your Script. Back at the command prompt, change to the directory where you saved the Perl script.