← Back to Perl Course | Chapter 1: Introduction | Lesson 3 of 7

Your first Perl program

A Perl program is a text file you hand to the perl command.

In this page:

  1. Your first Perl program

Your first Perl program

Save your code in a file that ends in .pl, then run it with perl filename.pl. Every statement ends with a semicolon, and print sends text to the screen. The special sequence \n at the end of a string starts a new line.

Note: On Unix-like systems you can add a first line such as #!/usr/bin/perl and make the file executable, so you can run it without typing perl.

Example: Your first Perl program

perl
#!/usr/bin/perl
use strict;
use warnings;

print "Hello, World!\n";
print "My first Perl program\n";
print "Script name: $0\n";

# Output:
# Hello, World!
# My first Perl program
# Script name: script.pl
Common Mistakes
  1. Forgetting the semicolon at the end of a statement
  2. Forgetting \n so that all output ends up on one line
  3. Running the script without the perl command when it is not executable
Chapter Summary
  • Save scripts as .pl files
  • Run them with perl script.pl
  • Statements end with semicolons
  • print writes text and \n ends a line
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.