How to create and run a Perl script in Debian 10

Introduction:

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. In this article, we will teach you the method of creating your first Perl script and running 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:

Step # 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 as shown in the image below:

Step # 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 as shown in the image below:

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 is shown in the image below:

Step # 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:

Method # 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:

Step # 1: 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

Step # 2: 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 as shown in the image below:

Method # 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 as shown in the image below:

Conclusion:

By following the procedures of creating and running a Perl script described in this article, you can make your life a lot easier by automating your commonly occurring tasks with the help of Perl scripting. This article just teaches you 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.



from Linux Hint https://ift.tt/3rT8QBe

Post a Comment

0 Comments