BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

First Perl Program

Last Updated: December 1, 2024 by Chaitanya Singh | Filed Under: Perl

In this tutorial, we will learn how to write first perl program and run it on various Operating Systems.

First Perl Program: Hello World

This is a simple perl program that prints Hello World!! on the screen.

#!/usr/bin/perl
#this is a comment
print "Hello World!!";

Here first instruction #!/usr/bin/perl tells Operating systems to run this program with the interpreter located at /usr/bin/perl.

As I explained in the perl introduction that perl is a high level language that uses normal english, the problem is that machine doesn’t understand this high level language , therefore in order to make this program understandable by machine, we must interpret it into machine language. Interpreter is what interpret this program into machine language.

Second instruction #this is a comment is a comment, whatever you write after hash(#) is considered as comment

Third instruction prints “Hello World!!”; prints Hello World!! on the screen. Note the semicolon(;) at the end of instruction, you must put a semicolon at the end of every instruction, this tells interpreter that the instruction is finished.

How to run the perl program?

In order to run the above program, copy the code into a text editor like notepad for windows, textedit for mac etc. and save the file with .pl extension. lets say we save the file as firstprogram.pl. You can save the program under any name. Perl doesn’t force you to use special kind of name, you are free to use any name you wish.

On Windows

To run the program on Windows, open command prompt (cmd) and write the following command

perl c:perlbinperl.exe firstprogram.pl

Note: The above method is for running the perl program using command prompt. Alternatively, if you are using Strawberry Perl or Active Perl then you can simply click run.

On Linux/Unix/Mac OS

To run the program on Linux, Unix or Mac OS. Run the following commands on terminal.

chmod +x firstprogram.pl
./firstprogram

How to specify version of Perl in the script?
There are certain cases where we want to specify the version of Perl to make our script work properly. For example, In perl 5.10 or later you can use “say” in place of “print”. This is the new feature which got introduced in Perl 5.10.

#!/usr/bin/perl
say "Hello World!!";

However this will not work if you are using Perl version prior to 5.10. To resolve this, you can re-write the above program like this:

#!/usr/bin/perl
use 5.010;
say "Hello World!!";

With the help of use statement we specified the version of Perl, this program will only work in Perl 5.10 or later.
Note: Perl expects sub version in three digits, that is the reason we have mentioned 5.010, if you use 5.10 instead then the Perl thinks that the mentioned version is 5.100. Similarly if you want to specify version 5.11 then use 5.011 instead of 5.11 in program.

❮ PreviousNext ❯

Top Related Articles:

  1. Perl Tutorial for beginners
  2. Use strict and use warnings in Perl
  3. my keyword – Local and global variables in Perl
  4. Perl Variables
  5. Installing Perl on Windows, Mac, Linux and Unix

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Perl Tutorial

  • Perl Tutorial
  • Perl Installation
  • First Perl Program
  • Perl Syntax
  • Data types in Perl
  • Perl Variables
  • my keyword
  • Perl Scalars
  • Use strict and use warnings
  • Perl Arrays
  • Perl Hashes
  • Operators in Perl
  • Perl Conditional statements
  • Perl if
  • Perl if-else
  • Perl if-elsif-else
  • Perl unless
  • Perl unless-else
  • Perl unless-elsif-else
  • Perl switch case
  • Perl given-when-default
  • Perl loops
  • Perl subroutines
  • Perl Strings
  • Perl Escape Sequences

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap