AnyCompiler

Your First Perl Program

Every real Perl script starts the same way: `use strict;` and `use warnings;`. Without them, Perl is remarkably forgiving — misspelled variable names silently create new ones, and dubious operations pass without comment. use strict turns the dangerous shortcuts into compile errors, and use warnings surfaces suspicious-but-legal code as runtime warnings. Virtually no production Perl code ships without both.

Comments start with # and run to the end of the line. print writes text to standard output — it doesn't add a trailing newline for you, so \n is written explicitly wherever one is wanted.

Run it yourself

Here's a small program with a comment and two print calls. This was actually executed to get the output shown below — try changing the text and running it yourself using the button under the example.

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

# This is a comment -- ignored by Perl
print "Hello, world!\n";
print "Learning Perl one step at a time.\n";
Output
Hello, world!
Learning Perl one step at a time.

What use strict actually catches

Without `my`, assigning to a variable Perl has never seen would normally just work — a new global springs into existence silently. Under use strict, that's a compile error instead, and Perl is specific about the fix. This is genuinely what Perl reports — captured from a real run, not written to look plausible:

use strict;
use warnings;

$greeting = "Hello without declaring!";
print "$greeting\n";
Output
stderr
Global symbol "$greeting" requires explicit package name (did you forget to declare "my $greeting"?) at script.pl line 4.
Global symbol "$greeting" requires explicit package name (did you forget to declare "my $greeting"?) at script.pl line 5.
Execution of script.pl aborted due to compilation errors.

Perl even tells you exactly what it thinks you forgot: declare it with `my`. Next: scalars and variables — my, lexical scoping, and what a scalar can hold.

Practice exercise

Add a comment above your code, then print "Learning Perl one step at a time."