As A Linux User, We Often Use Python And Bash Scripts. But When I Came Across This Question Of How To Execute Perl Script In Linux I Have Done Some Research And Found A Successful Method For Making, Running and executing Perl script. Here’s What I Found!
For Running Perl script in Linux, we just have to make or have Perl script with .pl extension. We can easily make the script through text editors like Vim, Nano, gedit, etc. Then we have to make our file executable by running the ” chmod +x example.pl ” Then We Can easily run the file by adding ./ before our file name like: “./example.pl” or through Perl command like “perl example.pl “
Let’s investigate this topic and understand a step-by-step method for executing Perl scripts in Linux. Let’s Get Started.
What Is Perl Script
Perl Script Is basically a file or program written in Perl programming language. Perl programming is known for its flexibility, text processing capabilities and use in system administration, automations and web development tasks. Perl Script Contains Instruction And Perl Interpreter run This instructions.
Step-by-step guide on creating and executing the Perl script
if you want to run something that means you also need something to run so in our case we need a Perl script. Making Perl script is as simple as creating Python, bash, and other scripts. In this post, we will create a simple “Hello World” saying perl file and will try to run it.
How to create a Perl script
For making a Perl script first we need to have a text editor where we can program our script for example vim, vs code, nano, gedit, etc. First, open your text editor and Program your script in our case for demo purposes I am making a simple script of Hello World.
Create a Perl script file:
You can directly create a new file at the terminal so firstly we have to open our terminal and then type this command
nano example.pl
So after successfully creating our Perl script file, we have to further program it by entering some coding in it in our case we are creating a simple โ Hello World โ telling script so to make this script we need to type this Code in our file.
#!/usr/bin/perl
print "Hello, World!\n";
If you don’t know how to write code in Perl script then you can learn the Perl programming from this tutorial : Perl Programming Tutorials By Tutorials point
So after programming and entering some code in our file, we have to save it and close the file. In our case we are using Nano text editor so this is done by pressing Control + X then Y, and entering. On the keyboard.
Making Our Perl File Executable
After successfully creating our Perl file putting code in our file and saving it now it’s time to give a script executable permissions. To run Our file make sure you have given your file executable permissions. To make our file or script executable we need to type this command
chmod +x example.pl
Running Perl script
Now we have successfully created our script for Hello World. also, we have given the executable permissions. now it’s time to run our script basically, there are two ways to run our Perl script on Linux
Direct Method
If this line: (โ #!/usr/bin/perl โ) is included in our script at the top then we can run the script directly by entering ./ before our file name This Line is known as shebang. So to run our script directly we have to type this command
./example.pl
Using the Perl interpreter
we can also use the Perl interpreter to execute our script even if the script is not marked as executable. So to use this method we have to run this command
perl example.pl
Now you can see we have successfully made our file & Made it executable and finally, We run the file.
Conclusion
So finally we saw how Easy it is to create an Perl file and making it executable. In this post we have successfully learned a method also we made files of a simple Hello World program also we made our file Executable and then finally we have learned two ways to Run our file. if you are facing any difficulties or any Errors then let me know in the comment section I will try to help you. if you found this post helpful then don’t forget to like it and rate it. And finally Thanks For Reading.
FAQs on how to execute Perl script in Linux
How to open perl file from command line
Simply use Nano before file name like ” nano example.pl ” also you can any text editor as you want simply write the text editors name and after that you file name.
How to create Perl script through Nano editor
For creating Perl file specifically By Nano text editor. You have to simple copy paste this command : ” nano yourfilename.pl “
How to make Perl file executable
Simply we can make our file executable by simply using chmod command : chmod +x example.pl
How to run Perl script
For Running Perl script there are 2 Method. First One Is by adding ./ before our file name and second one is by adding perl before our file name.
How to delete Perl script
Deleting file Whether It Is Perl File or Other file we can run the rm -rf command before our file name but make sure you are in the same directory. Ex : ” rm -rf yourfilename “