 |
|
| |
| Introduction
| |
Perl has been a Unix system-administration mainstay for decades, and it is still one of the best
tools for the everyday job of "read some text, pull out the parts that matter, and do something with
them" — exactly what log files, configuration files, and reports require. This page covers
installing Perl, writing a script, the one-liners that come up most often, and managing modules from
CPAN, the Comprehensive Perl
Archive Network.
| |
| Installing Perl
| |
Neither FreeBSD nor a minimal Rocky Linux installs the full Perl interpreter for you by default, so
add it from packages. Connect to your
VPS, become root, and install it:
On FreeBSD 15:
# pkg install perl5
$ perl -v # confirm the version
On Rocky Linux 10:
# dnf install perl
$ perl -v # confirm the version
Note the interpreter's location: on FreeBSD it is /usr/local/bin/perl; on Rocky Linux it is
/usr/bin/perl. That path is what goes on the first line of a script (below).
| |
| Writing a Script
| |
A Perl script is a plain text file. Make the first line a "shebang" pointing at the interpreter, mark
the file executable, and run it:
$ cat > hello.pl <<'EOS'
#!/usr/local/bin/perl
use strict;
use warnings;
my $name = shift // "world"; # first argument, or "world" if none
print "Hello, $name!\n";
EOS
$ chmod +x hello.pl
$ ./hello.pl Dan
Hello, Dan!
Begin every script with use strict; and use warnings; — they catch the typos and
undeclared-variable mistakes that cause most Perl bugs, and turn silent misbehavior into a clear
message. (On Rocky Linux, change the first line to #!/usr/bin/perl.)
| |
| One-Liners and Filters
| |
Where Perl really earns its keep on a server is one-liners and short filters. A few that come up
often:
# Count how many times each IP appears in an access log
$ perl -lane '$c{$F[0]}++; END{ print "$c{$_}\t$_" for sort {$c{$b}<=>$c{$a}} keys %c }' \
/var/log/httpd-example.com-access.log | head
# In-place find-and-replace across files (with a .bak backup of each)
$ perl -i.bak -pe 's/old\.example\.com/new.example.com/g' *.conf
# Pull every email address out of a file
$ perl -nE 'say $1 while /([\w.+-]+@[\w.-]+)/g' message.txt
The flags are worth learning once: -e runs the code on the command line, -n wraps it in
a loop over input lines, -p does the same but prints each line afterward (ideal for edits),
-l handles line endings, -a auto-splits each line into the @F array, and -i
edits files in place.
| |
| Managing CPAN Modules
| |
CPAN is Perl's enormous library of reusable modules — database drivers, web frameworks, date
handling, JSON, almost anything. There are three ways to install a module, and choosing the right one
avoids a lot of grief.
1. Prefer the system package when one exists. Most popular modules are packaged — with a
p5- prefix on FreeBSD, or a perl- prefix on Rocky Linux — so your normal upgrade
routine keeps them current and there is nothing to compile:
# FreeBSD
# pkg install p5-JSON p5-LWP-Protocol-https p5-DBD-mysql
# Rocky Linux
# dnf install perl-JSON perl-LWP-Protocol-https perl-DBD-MySQL
2. Use cpanm for modules that aren't packaged. App::cpanminus is a modern,
zero-configuration CPAN client — far friendlier than the traditional cpan shell:
# FreeBSD: pkg install p5-App-cpanminus | Rocky: dnf install perl-App-cpanminus
# cpanm Text::CSV # install system-wide (needs root)
$ cpanm --local-lib=~/perl5 Text::CSV # ...or into your own home directory
Installing into a personal directory with --local-lib is the safest habit: it keeps your
modules separate from the system Perl, so a package upgrade can't disturb them and you don't need root.
Tell Perl where to find them by adding this to your
shell startup file:
eval $(perl -I$HOME/perl5/lib/perl5 -Mlocal::lib=$HOME/perl5)
3. The classic cpan shell is always available as a fallback (cpan then
install Some::Module).
|
NOTE: Some modules are XS modules — they include C code and must be
compiled, so they need a C compiler and sometimes a system library's development files. If a
cpanm install fails, scroll up in its output (or read ~/.cpanm/build.log): the
real error is usually a missing library you can install with the package manager — on
Rocky Linux that often means the matching -devel package — after which the module
builds cleanly.
|
| |
| Documentation
| |
perldoc reads any module's documentation (perldoc Some::Module) and Perl's own guides
(perldoc perlintro). Online:
|
Toll Free 1-866-GSP-4400 • 1-301-464-9363 • service@gsp.com
Copyright © 1994-2026 GSP Services, Inc.
|