Hello World (first programs) · Beginner · 15 min
Hello World in Perl: strict, warnings and a one-liner
By Padding · Updated 18 Nov 2026
Perl 5.44.0 · perl:5.44
You will install Perl 5.44, run a Hello World script that has strict and warnings on, read a name from the keyboard, and summarise a log file with a one-line Perl command.
On this page
Perl is the language that made "text in, report out" a one-line job. It was released in 1987 by Larry Wall as a better tool for reading files and printing reports than the shell, sed and awk of the time, and that is still where it shines: regular expressions are part of the language itself, not a library you import, so matching, capturing and rewriting text takes very little code.
Two things about Perl surprise newcomers. First, every variable name starts with a symbol, called a sigil, that tells you what kind of value it holds: $ for one value, @ for a list, % for a table of names and values. Second, the same expression can give different results depending on whether Perl wants one value or a list from it. This is called context, and you see both in this tutorial.
You will check the Perl your system already has, install Perl 5.44, write and run Hello World with Perl's safety checks switched on, and read a name from the keyboard. Then you go one step further and answer questions about a log file with single-line Perl commands, which is the reason many system administrators still keep Perl close.
Perl is used for text processing, system administration scripts, bioinformatics (parsing large DNA and protein files was an early Perl success), and long-lived web systems such as the Request Tracker ticketing system. Its library archive, CPAN, has collected reusable modules since 1995.
Step 1 · Check the system Perl and install Perl 5.44
Most Linux systems and macOS come with a Perl already, because operating system tools use it. See what you have:
perl -vUbuntu 26.04 includes Perl 5.40.1. Leave that copy alone, since system packages depend on it, and install Perl 5.44 next to it.
- Ubuntu or macOS: perlbrew builds Perl inside your home folder and lets you switch between versions. It compiles Perl from source, so it needs a C compiler (
build-essentialon Ubuntu; on macOS runxcode-select --installfirst and skip theaptline) and takes several minutes:
sudo apt install -y build-essential curl
\curl -L https://install.perlbrew.pl | bash
echo 'source ~/perl5/perlbrew/etc/bashrc' >> ~/.bashrc
source ~/.bashrc
perlbrew install perl-5.44.0
perlbrew switch perl-5.44.0
perlbrew install-cpanmThe last line installs cpanm, a small tool for installing CPAN modules, which you will want after this tutorial. On macOS your shell is zsh: use ~/.zshrc instead of ~/.bashrc in the third and fourth lines.
- Docker instead of installing: if you already use Docker, the official
perl:5.44image has Perl 5.44 ready. From the folder that holds your script, rundocker run --rm -it -v "$PWD":/work -w /work perl:5.44 perl hello.plin place ofperl hello.pl. - Windows: the commands in this tutorial need a bash shell. They create files with
cat > file <<'PERL', usemkdir -p,chmod +xandrm -rf, and wrap one-liners in single quotes, none of which work in cmd.exe or PowerShell. To follow along, install WSL with Ubuntu (runwsl --installin an administrator PowerShell, then open the Ubuntu app) and use the perlbrew steps above inside it. Strawberry Perl, from strawberryperl.com, is the native Windows Perl and is fine for running scripts you have saved, withperl hello.plin cmd.exe. At the time of writing its newest stable build is Perl 5.42, and a 5.44 build is in testing, so writeuse v5.42;wherever the scripts sayuse v5.44;if you run them there.
Open a new terminal and check again:
perl -vCheck it worked
perl -v prints something like This is perl 5, version 44, subversion 0 (v5.44.0). If it still names version 40 or another older number, the terminal is finding the system Perl first: open a new terminal so the perlbrew line in ~/.bashrc runs, and check that which perl points into ~/perl5/perlbrew/.
Step 2 · Write hello.pl and run it
Make a practice folder and create hello.pl. Paste the whole block into your terminal (on Windows, the Ubuntu terminal in WSL). The cat > hello.pl <<'PERL' line writes everything up to the closing PERL marker into the file, which saves you opening an editor; the later steps create their files the same way.
mkdir -p ~/hello-perl-demo
cd ~/hello-perl-demo
cat > hello.pl <<'PERL'
#!/usr/bin/env perl
use v5.44;
say "Hello, world";
PERL
perl hello.plLine by line:
#!/usr/bin/env perlis the shebang line. On macOS and Linux it tells the system which program runs this file when you start it directly. To Perl it is a comment, because it starts with#.use v5.44;says this script needs Perl 5.44 or newer, and turns on that version's bundle of modern behaviour. Three parts of the bundle matter now: it switches onstrict, which makes you declare every variable before use; it switches onwarnings, which reports code that runs but probably does not do what you meant; and it enablessay. Older scripts writeuse strict;anduse warnings;on separate lines, and you will see that in a lot of existing code. It means the same thing.say "Hello, world";prints the text followed by a new line. Perl's olderprintdoes not add the new line, so you would writeprint "Hello, world\n";for the same result. Every statement ends with a semicolon.perl hello.plstarts the Perl interpreter. Perl compiles the whole file into an internal form first, so a syntax error anywhere stops the script before any line runs, and then it runs the result from top to bottom.
On macOS and Linux you can also make the file executable and run it by name, which uses the shebang line. perl -c checks the syntax without running anything:
chmod +x hello.pl
./hello.pl
perl -c hello.plCheck it worked
perl hello.pl and ./hello.pl both print Hello, world. perl -c hello.pl prints hello.pl syntax OK.
Step 3 · Let strict and warnings catch your mistakes
Perl without strict creates a new variable the first time you mistype a name, and carries on with an empty value. With strict on, a typo is an error. Make a script with a misspelt variable:
cat > typo.pl <<'PERL'
use v5.44;
my $name = "Amara";
say "Hello, $nmae";
PERLmy declares a variable. Here it declares $name, and the last line uses $nmae by mistake. Run it:
perl typo.plPerl refuses to start and prints something like Global symbol "$nmae" requires explicit package name (did you forget to declare "my $nmae"?) at typo.pl line 3. Fix the spelling and it prints Hello, Amara. Notice also that "Hello, $name" placed the variable's value inside the text: double-quoted strings fill in variables, single-quoted ones do not.
Warnings catch code that runs but is probably wrong. Perl has separate comparison operators for numbers (==) and for text (eq). Compare text with the number operator:
cat > compare.pl <<'PERL'
use v5.44;
my $answer = "yes";
if ($answer == "no") {
say "Perl thinks they match";
}
PERL
perl compare.plThe script runs, but prints warnings such as Argument "no" isn't numeric in numeric eq (==) at compare.pl line 3. and then Perl thinks they match. == turned both words into the number 0, so they compared equal. Change == to eq and run it again: the warnings go and nothing is printed, because "yes" and "no" are different text.
Check it worked
typo.pl stops with a Global symbol "$nmae" error and runs once you fix the spelling. compare.pl shows isn't numeric warnings with == and prints nothing, with no warnings, after you change it to eq.
Step 4 · Read a name from the keyboard
Create greet.pl, which asks for a name and counts its letters:
cat > greet.pl <<'PERL'
#!/usr/bin/env perl
use v5.44;
use open qw(:std :encoding(UTF-8));
print "What is your name? ";
chomp(my $name = <STDIN>);
my $letters = () = $name =~ /\X/g;
say "Hello, $name";
say "Your name has $letters letters.";
PERLLine by line:
use open qw(:std :encoding(UTF-8));tells Perl that the keyboard and screen use UTF-8 text. Without it Perl treats input as raw bytes, so a name with accents or tone marks would be counted wrong.qw(...)is a short way to write a list of words.print "What is your name? ";shows the question with no new line, so the answer goes on the same line.<STDIN>reads one line from the keyboard, including the new line at the end from pressing Enter.my $name = <STDIN>stores it in a new variable, andchomp(...)around that removes the trailing new line. Writing it as one line is the usual Perl habit.$name =~ /\X/gmatches the name against a regular expression.=~means "match this text against this pattern",\Xmatches one letter as a reader sees it, including any accent marks attached to it, and/gfinds every match, not just the first.my $letters = () = ...;counts the matches. The matches are assigned to an empty list(), and a list assignment used where Perl wants a single value gives the number of items. That is context at work, and you see more of it in the next step.
Run it and type your name:
perl greet.plA Yoruba name such as Adébáyọ̀ is a good test if your users write in Yoruba, Igbo, Vietnamese or another language with tone marks. It has 7 letters, but the last one is stored as two characters: ọ plus a separate combining grave accent. Perl's length($name) would report 8; \X groups the mark with its letter and reports 7, the number a person would give.
Check it worked
After you type Ada and press Enter, the script prints Hello, Ada and Your name has 3 letters. Typing Adébáyọ̀ prints Your name has 7 letters.
Step 5 · See sigils and context in one script
Create market.pl, which uses all three kinds of variable:
cat > market.pl <<'PERL'
#!/usr/bin/env perl
use v5.44;
use List::Util qw(sum);
my $city = "Lagos";
my @prices = (1500, 250, 4200);
my %stock = (rice => 12, beans => 0, garri => 30);
my $count = @prices;
my ($first) = @prices;
say "City: $city";
say "Second price: $prices[1]";
say "Number of prices: $count";
say "First in the list: $first";
say "Total: ", sum(@prices);
for my $item (sort keys %stock) {
say "$item: $stock{$item}";
}
PERL
perl market.plLine by line:
use List::Util qw(sum);loads a module that ships with Perl and imports itssumfunction. Modules that do not ship with Perl come from CPAN; with cpanm installed,cpanm Some::Moduledownloads and installs one.my $cityholds one value, a scalar, so it starts with$.my @pricesholds an ordered list, an array, so it starts with@. Items are numbered from 0.my %stockholds a hash: names paired with values.=>is a comma that also lets you leave the quotes off the name on its left.my $count = @prices;assigns an array to a scalar. Perl wants one value here, and an array asked for one value gives its length, 3.my ($first) = @prices;puts brackets around the variable, which makes it a list assignment. Now Perl hands over the list itself and$firstgets the first item, 1500. Same array, different context, different result.$prices[1]reads one item from the array. The sigil changes to$because one item is a single value: the sigil shows what you get back, not what the variable is.$stock{$item}reads one value from the hash, also with$. Hashes use curly brackets, arrays use square brackets.sort keys %stockgets the hash's names and sorts them. Hashes have no fixed order of their own, so sort when the order matters.
Check it worked
The script prints:
City: Lagos
Second price: 250
Number of prices: 3
First in the list: 1500
Total: 5950
beans: 0
garri: 30
rice: 12Step 6 · Answer questions about a log with one-liners
Perl can run a program written straight on the command line, and it can loop over every line of a file for you. Create a small log file to practise on:
cat > app.log <<'LOG'
2026-11-18 09:14:02 INFO path=/ status=200 ms=12
2026-11-18 09:14:05 ERROR path=/pay status=500 ms=830
2026-11-18 09:15:11 INFO path=/about status=200 ms=9
2026-11-18 09:16:40 WARN path=/pay status=429 ms=3
2026-11-18 09:17:02 ERROR path=/login status=500 ms=1204
2026-11-18 09:18:30 INFO path=/ status=200 ms=15
LOGShow only the error lines:
perl -ne 'print if /ERROR/' app.log-emeans "the program is the next argument" instead of a file.-nwraps that program in a loop that reads the file one line at a time and puts each line in$_, Perl's default variable.print if /ERROR/prints the current line if it matches the patternERROR. Bothprintand the match use$_when you name nothing else, which is why the program is so short.
Now count the requests for each status code:
perl -nE '$count{$1}++ if /status=(\d+)/; END { say "$_ $count{$_}" for sort keys %count }' app.log-Eworks like-ebut turns on the newest features, includingsay./status=(\d+)/matchesstatus=followed by one or more digits. The brackets capture the digits, and Perl puts them in$1.$count{$1}++adds one to the hash entry for that status code, creating it at 0 the first time.END { ... }runs once, after the last line has been read. It prints each status code and its count.
If you are running Strawberry Perl in cmd.exe instead of a bash shell, swap the quotes: cmd.exe does not treat single quotes as quoting, so put the program in double quotes and use qq{...}, Perl's other way of writing a double-quoted string, inside it. For example, perl -nE "$count{$1}++ if /status=(\d+)/; END { say qq{$_ $count{$_}} for sort keys %count }" app.log. This form does not work in PowerShell, which reads $ inside double quotes as its own variables.
One-liners do not have strict turned on, which is why %count needs no my. That trade-off suits throwaway commands; keep use v5.44; in any script you save.
Finally, add up the response times, and then find the slowest request:
perl -nE '$total += $1 if /ms=(\d+)/; END { say "Total ms: $total" }' app.log
perl -nE 'if (/path=(\S+).*ms=(\d+)/ and $2 > $max) { ($max, $path) = ($2, $1) } END { say "Slowest: $path ($max ms)" }' app.logThe same commands work on real logs of millions of lines, and because they read one line at a time they use little memory even on a small server.
Check it worked
The first command prints the two ERROR lines. The second prints 200 3, 429 1 and 500 2 on separate lines. The last two print Total ms: 2073 and Slowest: /login (1204 ms).
Step 7 · Fix the errors beginners hit
Wrong Perl version. If you run hello.pl with an older Perl, it stops at once with something like Perl v5.44.0 required--this is only v5.40.1. You are running the system Perl: check which perl, or open a new terminal so perlbrew's setup line runs.
A missing semicolon. Leave the ; off the end of a line and Perl reports syntax error at ... line N, near ..., often pointing at the line after the real mistake, because that is where Perl noticed. Look at the end of the line above first.
Uninitialized value. If input ends before a line arrives (press Ctrl+D on macOS or Linux, or Ctrl+Z then Enter on Windows, at the greet.pl prompt), <STDIN> returns undef, Perl's "no value", and the warnings report Use of uninitialized value $name. Write chomp(my $name = <STDIN> // ''); to use empty text instead: // means "if the left side has no value, use the right side".
Single quotes do not fill in variables. Only double-quoted strings replace $name with its value:
cat > quotes.pl <<'PERL'
use v5.44;
my $name = "Kofi";
say "Hello, $name";
say 'Hello, $name';
PERL
perl quotes.plThe first line prints Hello, Kofi; the second prints Hello, $name exactly as written.
When you have finished, remove the practice folder:
cd ~
rm -rf ~/hello-perl-demoCheck it worked
quotes.pl prints Hello, Kofi and then Hello, $name literally. ls ~ no longer shows hello-perl-demo.
Next steps
- Write the same kind of first script in the shell that Perl was built to improve on: Hello World in Bash.
- Try Ruby, which took many ideas from Perl, including
=~for regular expressions: Hello World in Ruby. - Do for JSON what the one-liners here did for plain log lines: process JSON on the command line with jq.
Related tutorials
- Hello World in Bash: a script with arguments and exit codesYou will write and run a Bash 5.3 script that greets a name from the command line or the keyboard, rejects bad input with its own exit code, runs in strict mode and passes ShellCheck.
- Hello World in Ruby: irb, puts and a first gem with BundlerYou will install Ruby 4.0, see how puts, print and p differ in irb, run a script that greets the user with a block, and print a table with your first gem managed by Bundler.
- How to Process JSON on the Command Line with jqYou will install jq 1.8, read values out of a JSON file, filter and reshape arrays, total and group data, turn JSON into CSV, pass shell values in safely, change a document, work with files of one JSON object per line, and use jq's exit status in scripts.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials