Perl Hello World Example: How To Write and Execute Perl Program on Unix OS

by Ramesh Natarajan on September 25, 2009

Question: I would like to understand the basics of how to write and execute Perl program on Linux OS. Can you explain it with a simple example?

Answer: In this article, let us review very quickly how to write a basic Hello World Perl program and execute *.pl program on Linux or Unix OS.

1. Write a Hello World Perl Program

Create the helloworld.pl program using a Vim editor as shown below.

$ vim helloworld.pl

#!/usr/bin/perl

# Hello world perl program

print "Hello World!";

2. Make sure Perl Interpreter is installed on your system

Make sure perl interpreter is installed on your system as shown below.

$ whereis perl
perl: /usr/bin/perl /etc/perl /usr/lib/perl
/usr/share/perl /usr/share/man/man1/perl.1.gz

$ which perl
/usr/bin/perl

3. Execute the Perl Program

You can either execute using “perl helloworld.pl” or “./helloworld.pl”.

$ perl helloworld.pl
Hello World!

( or )
$ chmod u+x helloworld.pl

$ ./helloworld.pl
Hello World!

Note: As perl is an interpreted language, you don’t have the compilation step similar to the C program.

4. Writing and Executing Perl One liner

You can also execute perl from the command line as shown below. This will print Hello World!.

perl -e 'print "Hello World!\n"'
Download Free eBook - Linux 101 Hacks

Get free Unix tutorials, tips and tricks straight to your email in-box.

If you enjoyed this article, you might also like..

  1. Lua Hello World Example: How To Write and Execute Lua Program on Linux OS
  2. Ruby Hello World Example: How To Write and Execute Ruby Program on Unix OS
  3. Python Hello World Example: How To Write and Execute Python Program on Unix OS
  4. Tcl Hello World Example: How To Write, Compile and Execute Tcl Program on Linux OS
  5. Lisp Hello World Example: How To Write and Execute Lisp Program on Linux OS
  

Vim 101 Hacks Book

Leave a Comment

Previous post:

Next post: