In this section, we will write our first Perl program: the classic "Hello, World!" example. This simple program will help you understand the basic structure of a Perl script and how to execute it.

Key Concepts

  1. Perl Script Structure: Understanding the basic structure of a Perl script.
  2. Print Function: Using the print function to output text.
  3. Shebang Line: The importance of the shebang line in Perl scripts.
  4. Running a Perl Script: How to execute a Perl script from the command line.

Step-by-Step Guide

  1. Basic Structure of a Perl Script

A Perl script typically consists of:

  • A shebang line (optional but recommended for Unix-like systems).
  • Perl code.

  1. The Shebang Line

The shebang line tells the system which interpreter to use to execute the script. It is the first line in the script and starts with #!.

Example:

#!/usr/bin/perl

  1. The Print Function

The print function is used to output text to the screen. In our "Hello, World!" program, we will use print to display the message.

Example:

print "Hello, World!\n";

  1. Writing the "Hello, World!" Program

Let's put it all together. Open your text editor and type the following code:

#!/usr/bin/perl
# This is a simple Perl script to print "Hello, World!"

print "Hello, World!\n";

  1. Saving the Script

Save the file with a .pl extension, for example, hello_world.pl.

  1. Running the Script

To run the script, follow these steps:

  1. Open your terminal or command prompt.
  2. Navigate to the directory where you saved hello_world.pl.
  3. Make the script executable (Unix-like systems only):
    chmod +x hello_world.pl
    
  4. Execute the script:
    ./hello_world.pl
    

On Windows, you can run the script directly using:

perl hello_world.pl

Example Output

When you run the script, you should see the following output:

Hello, World!

Practical Exercise

Exercise 1: Modify the Hello World Program

  1. Modify the "Hello, World!" program to print your name instead of "World".
  2. Save the modified script as hello_name.pl.
  3. Run the script to ensure it works correctly.

Solution

Here is the modified script:

#!/usr/bin/perl
# This is a simple Perl script to print "Hello, [Your Name]!"

print "Hello, [Your Name]!\n";

Replace [Your Name] with your actual name. For example:

#!/usr/bin/perl
# This is a simple Perl script to print "Hello, Alice!"

print "Hello, Alice!\n";

Run the script using the same steps as before. You should see:

Hello, Alice!

Common Mistakes and Tips

  • Missing Shebang Line: If you forget the shebang line, the script may not run correctly on Unix-like systems.
  • Syntax Errors: Ensure you use the correct syntax for the print function, including the semicolon at the end of the line.
  • File Permissions: On Unix-like systems, make sure the script is executable by using chmod +x.

Conclusion

In this section, you learned how to write and run a simple Perl script that prints "Hello, World!" to the screen. This foundational knowledge will help you as you progress through more complex Perl programming concepts. Next, we will dive into the basic syntax and structure of Perl scripts.

© Copyright 2024. All rights reserved