Your First Perl Script
Write and run your first complete Perl script: the shebang line, use strict and use warnings, and how to execute a script from the command line.
Introduction
With Perl installed, it is time to write actual code. This lesson walks through creating, saving, and running a real Perl script from start to finish, explaining every line so nothing feels like unexplained "magic."
- How to create and save a .pl script file.
- What the shebang line does and why it matters on Linux/macOS.
- Why every professional script starts with use strict; and use warnings;.
- How to run a Perl script from the command line.
Creating Your Script File
Perl scripts are plain text files, conventionally saved with a .pl extension. Create a new file called hello.pl in a folder of your choice using your editor of choice.
mkdir perl-practicecd perl-practicetouch hello.plIf touch is not available in your shell, simply create a new file named hello.pl directly from your editor (for example, File > New File in VS Code, then save it as hello.pl).
The Shebang Line
On Linux and macOS, the very first line of an executable script can tell the operating system which interpreter should run the file. This special line, called the shebang (from "hash-bang," referring to the # and ! characters), always starts in column one of the file.
#!/usr/bin/perlThis tells the shell: "run this file using the Perl interpreter located at /usr/bin/perl." A more portable variant searches your PATH for whichever perl comes first, which is useful if Perl is installed somewhere other than /usr/bin.
#!/usr/bin/env perlOn Windows, the shebang line is not used by the operating system to launch scripts, since you typically run scripts with perl script.pl directly. It is still good practice to include it anyway, both for documentation and for portability if the script is later run on Linux or macOS.
use strict and use warnings
These two lines, called pragmas, change how strictly Perl checks your code while it runs.
- use strict; forces you to declare variables before using them (with my, covered soon) and disallows several error-prone shortcuts that older Perl code relied on.
- use warnings; makes Perl print helpful diagnostic messages when it notices something suspicious, such as using an uninitialized value or comparing incompatible types.
Without these two lines, Perl will often run broken code silently, producing wrong results with no explanation. With them enabled, many bugs are caught immediately, with a specific line number, instead of causing confusing behavior later.
use strict;use warnings;
# Without 'use strict', this next line would silently create a# global variable even if $nam was a typo for $name.my $name = "Perl Learner";print "Hello, $name!\n";Click Run to see what this code prints.
Writing the Hello World Script
Now put it all together. Open hello.pl and enter the following complete script.
#!/usr/bin/perluse strict;use warnings;
print "Hello, World!\n";print "Welcome to Perl.\n";Every element here has a purpose: the shebang for Linux/macOS execution, the two safety pragmas, and two print statements that each end with a semicolon and include \n to move to a new line afterward.
Running Your Script
The simplest and most portable way to run any Perl script, on any operating system, is to invoke the perl interpreter directly and pass it your filename.
perl hello.plClick Run to see what this code prints.
This works identically on Windows, macOS, and Linux, and is the method used throughout this entire course.
Making a Script Executable (Linux/macOS)
On Linux and macOS, thanks to the shebang line, you can also mark a script as executable and run it directly by name, without typing perl in front of it.
chmod +x hello.pl./hello.plClick Run to see what this code prints.
chmod +x grants the file execute permission, and ./hello.pl tells the shell to run the file in the current directory. This is a common convention for standalone command-line tools written in Perl.
Common Mistakes
- Forgetting the semicolon at the end of a statement, which produces a syntax error.
- Leaving out use strict; and use warnings;, hiding bugs that would otherwise surface immediately.
- Putting the shebang line anywhere other than the very first line of the file — it must be line 1, column 1.
- Trying to run ./hello.pl on Linux/macOS without first making it executable with chmod +x.
Best Practices
- Start every script, without exception, with use strict; and use warnings;.
- Save Perl scripts with the .pl extension so editors and tools recognize the file type.
- Prefer #!/usr/bin/env perl over a hardcoded path for better portability across machines.
- Run scripts with perl script.pl while learning, since it works identically everywhere.
Frequently Asked Questions
It is not required to run the script on Windows since you invoke perl directly, but including it is still considered good practice for cross-platform portability and clarity.
Perl will usually report a syntax error pointing at or near the line after the missing semicolon, since it cannot tell where one statement ends and the next begins.
Yes, absolutely. A script is just a sequence of statements executed in order from top to bottom, and print can be called as many times as needed.
Key Takeaways
- A Perl script is a plain text file, conventionally saved with a .pl extension.
- The shebang line tells Linux/macOS which interpreter runs the file when executed directly.
- use strict; and use warnings; should appear at the top of virtually every script you write.
- Run any script portably with perl script.pl, or directly with ./script.pl after chmod +x on Linux/macOS.
Summary
You have now written, saved, and executed a real Perl script. Next, we will look more closely at how a Perl program is structured — statements, semicolons, comments, and the print function — before moving on to variables.