This tutorial explains about

  • Perl First Program
  • How to execute Perl scripts in the terminal

Perl Hello World Program Example

To create a Perl Program code, Please follow the below steps

  • Open any text editor Such as Notepad or VSCode
  • Add the below lines of code, and save this file as hello.pl
  • hello is the script file name, and pl is a file extension for Perl code statements.
#!/usr/bin/perl
use strict;
use warnings;
# hello.pl
# Hello world test program
print "Hello World program example";

Perl contains code statements on each line. Each line must end with either ; or {}./

Perl hello world program contains below parts

  • shebang line

Perl program contains an optional first line as shebang. It is the location of the Perl executable version and tells which version to use for compiling the Perl script files.

#!/usr/bin/perl
  • Pragma Statements

These are the first statements to tell the Perl compiler to enable warning behavior at compile and runtime. It instructs the compiler to give strict warnings to the user when code runs with these lines included. It is Optional to include

use strict;
use warnings;
  • Comments

the line starts with # are comments. Comments are ignored by the interpreter. You can write comments in a single or end of code as inline.

  • Print Print is a Perl function that prints a string to the console

To execute and run a Perl code program,

  • Run, the below command
Perl hello.pl

It prints a message to the console.

Another way to execute a single-line statement using the Perl command.

perl -e "Hello World"

It prints a message to the console.

How to run Hello World in Perl?

To run the Hello World program, please follow the below steps

  • First Open a Text editor such as Notepad

  • Next, Create a new file

  • Add the below lines

    print "hello world"
    
  • Save the file as HelloWorld.pl. pl is an extension.

  • Run the per program using the Perl HelloWorld.pl command in terminal

What is the command for Hello, World?

You can also run Perl code in the command line using Perl with the -e option. Open a terminal and run the below command

perl -e "Hello World"
  • with the -e option, you can run code inline single statements.

How do I print Hello World on standard output using Perl?

To print Hello World to standard output, follow the below steps. There are two ways to print Hello World to stand output.

  • First, Create a Perl script file with the print statement, next, Run the Perl script file
  • Second, Run Perl -e “Hello World” to print to the console.

How do I start a Perl script?

Perl scripts are programs that contain Perl code block statements in the pl extension. You can run Perl scripts using the Perl script command.