The First Perl Program

In this page, we can see how a perl program looks and how it is executed, we will also look at the syntax, few rules while writing a perl program.

Lets have a quick peek of a Perl Program and see how does it look

To write a perl program, open up your favourite text-editor.

Note: Do remember to save all perl programs with ".pl" extension

[gray@ckserver Perl Programming]$ ls -l
-rw-rw-r--. 1 gray gray 675 Aug  9 08:42
[gray@ckserver Perl Programming]$
[gray@ckserver Perl Programming]$ chmod 774
[gray@ckserver Perl Programming]$
[gray@ckserver Perl Programming]$ ls -l
-rwxrwxr--. 1 gray gray 675 Aug  9 08:42
[gray@ckserver Perl Programming]$ ./
Enter Your Name : Subir Sutradhar
Hello, Subir Sutradhar
! How are you..
Hello again Subir Sutradhar, Welcome to our Perl Basics Tutorials
[gray@ckserver Perl Programming]$
[gray@ckserver Perl Programming]$
[gray@ckserver Perl Programming]$ cat
# Description        :    The First Hello World Program
# Date             :    9th Aug, 2014
# Author         :    Subir Sutradhar

use strict; # An important line

use warnings; # Another important line

print "Enter Your Name : "; # print a question to the default STDOUT ie. screen

my $name; # declare a variable in memory named $name

$name = <STDIN>; # Take input from default input ie. Keyboard

print "Hello, $name! How are you..\n"; # printing to the screen

chomp($name); # removes new line character from the end of the user input

print "Hello again $name, Welcome to our Perl Basics Tutorials\n";

# Program Ends

[gray@ckserver Perl Programming]$

Lets understand the program now..

Interpreter Location : The first line "#!/usr/bin/perl" tells the path where the perl interpreter is located and this should be the first line of any perl program, NO OTHER LINE NOT EVEN A COMMENT SHOULD BE WRITTEN BEFORE THIS LINE.

Comments/Remarks : We can make comments or remarks like every other programming language, using the "#" symbol,which implies that any line that starts with "#" symbol is a comment line.

Note: The # symbol in the very first line tells about the path to perl interpreter and is not a comment symbol.

Every Statement in a perl program ends with a semi-colon ";"

Next, in the program, "use strict" and "use warnings" are two important lines very useful for new perl programmers, which changes rule to interpret a perl script.

use strict - Informs the interpreter to use all rules to be followed syntactically in the perl program, if any mistakes found it will inform during the compile time.

use warnings - Causes the program to give warnings if something iffy or confusing is found by the perl interpreter during the run time of the perl script.

With that said, now we understand the basic syntax of a perl script, which is easy.

Executing a perl program:

A newly created script will not have the execute bit turn on, until umask in your system is defined in such a way that the execute bit turns on by default, which is really a bad practise security wise.

Anyways, if it is not on, we can either turn it on or execute the perl program in the following way..

[gray@ckserver Perl Programming]$ ls -l
-rw-rw-r--. 1 gray gray 675 Aug  9 08:42
[gray@ckserver Perl Programming]$ perl
Enter Your Name : ^C
[gray@ckserver Perl Programming]$

To turn on the execute bit:
[gray@ckserver Perl Programming]$ chmod +x


[gray@ckserver Perl Programming]$ chmod 774

and then,

[gray@ckserver Perl Programming]$ ./

Thats all for this part, hope you found it interesting.

Thank you for reading.

For any doubts feel free to comment....

Feel Free To Leave A Comment
If Our Article has Helped You, Support Us By Making A Small Contribution, Thank You!