How to Use Perl on Windows (Install, Run Scripts, CPAN, Editors, Best Practices)
Question
how to use perl in windows
Category: Perl / Development Tools
Difficulty: Beginner
Tags: windows, perl, strawberry-perl, activeperl, cpan, cpanminus, powershell, cmd, vscode, encoding, paths
Overview
Perl runs very well on Windows, but the “Windows way” has a few differences from Linux/macOS: file paths use backslashes, shells (CMD vs PowerShell) quote arguments differently, line endings are usually CRLF, and installing modules sometimes requires a compiler toolchain. Once you choose a good Perl distribution and verify your PATH, the daily workflow is straightforward: write .pl scripts, run them with perl script.pl, install modules from CPAN, and develop comfortably in an editor like VS Code.
1) Choose and Install a Perl Distribution
On Windows, the main choices are:
- Strawberry Perl: The most common choice for developers. It aims to be “CPAN-friendly” and typically includes a GCC toolchain (MinGW) so XS modules can compile. This is often the easiest path if you plan to install modules.
- ActivePerl: A commercial-backed distribution (ActiveState) with its own ecosystem and tooling. It can be a good fit in enterprise environments, but module installation flow may differ.
- WSL (Windows Subsystem for Linux): Not “Perl on Windows” exactly, but a Linux environment on Windows. If you need Linux parity for deployment, WSL can be a great option. You’d install Perl via the Linux package manager.
Typical recommendation: Install Strawberry Perl unless you have a specific reason not to.
Installation checklist
- Install a 64-bit Perl unless you must integrate with 32-bit-only components.
- During installation, ensure Perl is added to your PATH (installer option).
- Avoid installing into a path with unusual permissions. Default locations are usually fine.
2) Verify Perl Works (CMD and PowerShell)
Open Command Prompt (CMD) or PowerShell and run:
perl -v
You should see Perl version information. Also useful:
where perl
perl -e "print qq(Perl OK\n)"
If perl is “not recognized,” your PATH isn’t set. Re-run the installer and enable “Add Perl to PATH,” or manually add the Perl bin directory to PATH via Windows Environment Variables.
3) Running Perl Scripts
Perl scripts are plain text files, typically ending in .pl. You run them like:
perl script.pl
You can pass arguments which become available in @ARGV. On Windows, be mindful of quoting rules:
- CMD quoting is relatively simple but inconsistent for special characters.
- PowerShell has its own parsing rules; single quotes are literal, double quotes interpolate.
Example 1: Basic script + command-line arguments
use strict;
use warnings;
my $name = shift(@ARGV) // "World";
print "Hello, $name!\n";
Run:
perl hello.pl Windows
Expected output:
Hello, Windows!
4) Installing Modules (CPAN) on Windows
Perl’s library ecosystem is CPAN. On Windows, the key question is whether a module requires compilation (XS). Strawberry Perl typically includes the tooling to compile many modules; that’s one reason it’s popular.
Recommended approach: cpanminus (cpanm)
cpanm is a lightweight CPAN installer. You can install it using the standard cpan client once:
cpan App::cpanminus
Then install modules like:
cpanm JSON::XS
cpanm DBD::Pg
If a module fails to build, read the error carefully—common causes include missing compiler tools (less likely on Strawberry), missing external libraries (e.g., database client headers), or architecture mismatch (32-bit vs 64-bit).
Project-local dependencies (best practice)
Instead of installing modules system-wide, many teams prefer per-project installs. On Perl, the common approach is local::lib which installs modules under a directory you control (often in your home directory or within a project folder):
cpanm local::lib
Then follow its instructions to set environment variables (like PERL5LIB and PATH updates). This helps keep projects isolated and reduces “it works on my machine” issues.
5) Writing Perl Comfortably on Windows (Editors + Tooling)
You can use any editor. A very common setup is:
- VS Code + a Perl extension for syntax highlighting and linting
- Integrated terminal (PowerShell or CMD) to run scripts
perltidyfor formatting (optional but helpful)
Handy built-in documentation tools:
perldoc perlintro(intro)perldoc perlfunc(functions)perldoc -f open(docs foropen)perldoc -q "how do I"(FAQ search)
6) Best Practices (Especially Important on Windows)
Always use strict and warnings
They catch typos and logic mistakes early:
use strict;
use warnings;
Use cross-platform path handling
Windows paths use backslashes (C:\Temp\file.txt), but hardcoding separators is fragile. Use File::Spec (core module) to build paths safely across platforms:
use strict;
use warnings;
use File::Spec;
my $path = File::Spec->catfile("C:", "Temp", "example.txt");
print "$path\n";
Be explicit about text encoding
A common Windows pitfall is encoding: files might be UTF-8, UTF-8 with BOM, or even UTF-16. Also, the Windows console code page may not be UTF-8 by default. For scripts that read/write text files, prefer explicit layers:
use strict;
use warnings;
use open qw(:std :encoding(UTF-8));
If you work with Unicode heavily on Windows terminals, you may need to configure the terminal for UTF-8 (for example, using Windows Terminal and setting appropriate encoding). The exact steps vary by terminal and Windows version.
Prefer list-form system calls
Shell quoting and escaping differs between CMD and PowerShell. If you invoke external commands, avoid string-form system("..."). Prefer list-form system($cmd, @args) so Perl passes arguments safely without shell interpretation.
Keep scripts portable
- Avoid assuming
/tmp,/bin, or Unix utilities exist on Windows. - Use core modules like
File::Tempfor temp files. - Use
File::SpecandCwdto manage paths.
7) Common Pitfalls on Windows (and How to Avoid Them)
CRLF vs LF line endings
Windows typically uses CRLF (\r\n) line endings. Perl generally handles this fine in text mode, but bugs appear when you mix binary mode, network protocols, or do exact byte comparisons. If you need exact bytes, open files in binary mode (binmode) and handle newlines intentionally.
Backslashes in strings
In Perl double-quoted strings, backslash starts escapes. For Windows paths, either:
- Use single quotes:
'C:\\Temp\\file.txt'(still needs doubling backslashes) - Or use forward slashes where acceptable:
C:/Temp/file.txt(many Windows APIs accept it) - Or avoid manual paths and use
File::Spec
Spaces in paths
C:\Program Files\... breaks naive command strings. Use list-form system calls and quote carefully in shell commands.
Module build failures
Some CPAN modules rely on external C libraries or headers. If a module fails:
- Confirm you’re using a distribution with a compiler toolchain (Strawberry helps here).
- Check whether the module has a “PP” pure-Perl alternative (e.g.,
JSON::PPis core). - Match architecture: 64-bit Perl with 64-bit libraries.
8) Example 2: Create a file with CRLF, then count lines/words
This script demonstrates predictable file I/O on Windows and highlights CRLF explicitly.
use strict;
use warnings;
my $filename = "sample.txt";
# Write a small file using explicit CRLF newlines
open(my $out_fh, ">", $filename) or die "open for write: $!";
print {$out_fh} "alpha beta\r\n";
print {$out_fh} "gamma\r\n";
close($out_fh) or die "close: $!";
# Read it back in text mode and count
open(my $in_fh, "<", $filename) or die "open for read: $!";
my ($lines, $words) = (0, 0);
while (my $line = <$in_fh>) {
$lines++;
my @w = ($line =~ /\S+/g);
$words += scalar(@w);
}
close($in_fh) or die "close: $!";
print "lines=$lines words=$words\n";
Expected output:
lines=2 words=3
9) Example 3: JSON handling with a core module (no installs required)
JSON::PP is a pure-Perl JSON implementation that is commonly available as a core module in many Perl distributions. This example is useful on Windows because it avoids compilation and external dependencies.
use strict;
use warnings;
use JSON::PP qw(encode_json decode_json);
my $input = '{"name":"Ada","nums":[2,3,5]}';
my $data = decode_json($input);
my $sum = 0;
$sum += $_ for @{ $data->{nums} };
my %out = (
name => $data->{name},
sum => $sum,
);
print encode_json(\%out), "\n";
Expected output:
{"name":"Ada","sum":10}
10) Practical Workflows on Windows
Quick one-liners
Perl excels at one-liners for text processing. In PowerShell/CMD:
perl -ne "print if /error/i" app.log
Scheduling
Use Windows Task Scheduler to run a Perl script at intervals. The action typically calls perl.exe with the script path, e.g.:
"C:\\Strawberry\\perl\\bin\\perl.exe" "C:\\path\\to\\job.pl"
Packaging and distribution
If you need to ship a Perl script to machines without Perl installed, tools like PAR::Packer can bundle Perl + dependencies into an executable. On Windows this can simplify deployment, but it adds build complexity—test thoroughly on a clean machine.
Summary
To use Perl on Windows: install a solid distribution (commonly Strawberry Perl), ensure perl is on PATH, run scripts with perl script.pl, install dependencies via CPAN (ideally using cpanm and/or local::lib), and follow best practices that prevent Windows-specific issues: handle paths via File::Spec, be deliberate about encoding, and avoid fragile shell quoting by using list-form system calls. With these habits, Perl is a fast, reliable tool for automation, scripting, and data processing on Windows.
Verified Code
Executed in a sandbox to capture real output. • 10ms
lines=2 words=3
(empty)