Search⌘ K

Hello World

Explore how to write your first Perl program using the classic Hello World example. Understand the basic Perl syntax, the use of print and printf to display text, and how statements are structured with semicolons. This lesson helps you build a foundation for Perl scripting and output handling.

We'll cover the following...

Why “Hello World”?

The first program that most aspiring programmers write is the classic and the simplest “Hello World” program. The purpose of this program is to display the text “Hello World” on the screen.

Syntax

Try running the code below:

Perl
print "Hello World";

In the above example, we have used the print keyword to display the text Hello world on the screen. Programmers familiar with the C programming language will be happy to know that the C printf() function also works in Perl. So, the syntax would look like:

Perl
printf("%s", "Hello World");

In the above code, printf is used to display the text. The %s is used as a format specifier to let the command know that a string will be printed (which in this case is “Hello World”).

Key takeaways

  • print or printf keyword is used to display text
  • Any text that we want to print goes in pair of double quotes " "
  • Every statement in Perl ends at ; unless it is the final statement in a block, in which case the semicolon is optional