Perl First Program


Interactive Mode Programming

You can use Perl interpreter with -e option at the command line, which lets you execute Perl statements from the command line. Let's try something at $ prompt as follows −

$perl -e 'print "Hello World\n"'

This execution will produce the following result −

Hello, world

Script Mode Programming

Assuming you are already on $ prompt, let's open a text file hello.pl using vi or vim editor and put the following lines inside your file.

Example

 Live Demo

#!/usr/bin/perl
# This will print "Hello, World"
print "Hello, world\n";

Here /usr/bin/perl is actual the Perl interpreter binary. Before you execute your script, be sure to change the mode of the script file and give execution privilege, generally a set of 0755 works perfectly and finally you execute the above script as follows −

$chmod 0755 hello.pl
$./hello.pl

Output

This execution will produce the following result −

Hello, world

You can use parentheses for functions arguments or omit them according to your personal taste. They are only required occasionally to clarify the issues of precedence. The following two statements produce the same result.

print("Hello, world\n");
print "Hello, world\n";

Updated on: 28-Nov-2019

240 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements