LinuxCommandLibrary

perl

Execute Perl scripts

TLDR

Print lines from stdin [m/] matching regex1 and case insensitive [/i] regex2

$ perl -n -e 'print if m/[regex1]/ and m/[regex2]/i'
copy

Say [-E] first match group, using a regexp, ignoring space in regex [/x]
$ perl -n -E 'say $1 if m/[before] ( [group_regex] ) [after]/x'
copy

[-i]n-place, with backup, [s/] substitute all occurrence [/g] of regex with replacement
$ perl -i'.bak' -p -e 's/[regex]/[replacement]/g' [path/to/files]
copy

Use perl's inline documentation, some pages also available via manual pages on Linux
$ perldoc perlrun ; perldoc module ; perldoc -f splice; perldoc -q perlfaq1
copy

SYNOPSIS

perl [-swTUEO] [-hvLC] [-dt] [-I dir] [-Mmodule] [-mmodule] [-Fpattern] [-n] [-p] [-a] [-e command] [-i[extension]] [-0[octal]] [-s] [--] [programfile] [arguments]

PARAMETERS

-a
    Autosplit mode. When used with -n or -p, splits each input line into the @F array.

-C [list]
    Enables UTF-8 features in layers specified by list.

-d
    Runs the script under the Perl debugger.

-e command
    One line of program. Can be given multiple times to build up a multi-line program.

-F pattern
    Specifies the pattern to split on if -a is used.

-h
    Prints usage information.

-i[extension]
    Edits files in place. Optional extension backs up the original file.

-I directory
    Specifies a directory to search for modules.

-Mmodule
    Imports the specified module.

-mmodule
    Imports the specified module.

-n
    Assumes a 'while (<>) { ... }' loop around your script.

-p
    Like -n, but also prints the content of $_ after each iteration.

-s
    Enables rudimentary switch parsing (switches are prepended with hyphens).

-S
    Looks for the script using PATH environment variable.

-T
    Enables taint checking, which helps to write secure programs.

-U
    Allows Perl to do unsafe operations.

-v
    Prints Perl version information.

-w
    Enables warnings. Recommended for all Perl scripts.

-0[octal]
    Specifies the input record separator (end-of-line character). Useful for handling records delimited by null characters or other non-standard terminators.

--
    Signals the end of options. Any arguments following -- are treated as filenames or arguments to the script.

DESCRIPTION

Perl is a high-level, general-purpose, interpreted, dynamic programming language. It's known for its powerful text processing capabilities, making it a favorite for system administration, web development, and network programming.

Perl's syntax is sometimes described as "TIMTOWTDI" (There Is More Than One Way To Do It), reflecting its flexibility and allowing programmers to choose the most appropriate solution for a given problem. It incorporates features from other languages like C, sed, awk, and sh.

The Perl interpreter reads and executes Perl source code directly, without requiring compilation. Perl has a large module library, the Comprehensive Perl Archive Network (CPAN), that provides ready-made solutions to a vast array of tasks.

While Perl's popularity has waned somewhat with the rise of newer languages, it remains a powerful and relevant tool, especially for legacy systems and text-heavy applications.

CAVEATS

Perl's syntax can be complex and sometimes difficult to read. Taint checking (-T) is important for security, but can also introduce complexity.

ENVIRONMENT VARIABLES

Perl uses several environment variables, including PERL5LIB (specifies module search paths), PERL5OPT (default options), and PERL_DEBUG (enables debugging features).

CPAN

The Comprehensive Perl Archive Network (CPAN) is a vast repository of Perl modules and scripts contributed by the Perl community. It significantly extends Perl's functionality and provides pre-built solutions for many common tasks.

HISTORY

Perl was created by Larry Wall in 1987. Initially intended as a scripting language to make report processing easier, it evolved into a powerful general-purpose language.

Its strengths in text manipulation and system administration led to widespread adoption, especially in the early days of the World Wide Web.

Perl 5, released in 1994, was a major rewrite and established the language's core features and the CPAN module repository. While Perl 6 (now Raku) was a significant divergence, Perl 5 remains widely used and actively maintained.

SEE ALSO

awk(1), sed(1), grep(1)

Copied to clipboard