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 regex, 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 [switches] [--] [programfile] [arguments]
perl [switches] -e 'command' [arguments]
Explanation:
switches: Command-line options modifying Perl's behavior.
--: Marks the end of command-line switches, treating subsequent arguments as program arguments.
programfile: The path to a Perl script to execute.
arguments: Arguments passed to the Perl script or command.
-e 'command': Executes the Perl code specified directly on the command line.

PARAMETERS

-e command
    Executes the specified Perl command directly. Useful for one-liners.

-l[digits]
    Enables automatic newline translation. Often used with -n or -p to chomp input and add a newline to print output.

-n
    Wraps the script in a while (<>) { ... } loop, processing input line by line without printing by default.

-p
    Similar to -n, but also prints each line after processing it.

-i[extension]
    Enables in-place editing of files. If extension is provided, a backup of the original file is created.

-w
    Enables all standard warnings. Highly recommended for development.

-c
    Checks the syntax of the script without executing it.

-Mmodule
    Loads a specific Perl module before executing the script. Can include arguments, e.g., -Mstrict.

-T
    Enables taint mode, which performs security checks on data derived from outside the program (e.g., command-line arguments, environment variables). Essential for CGI scripts.

-v
    Prints the Perl version and patchlevel information.

-V[configvar]
    Prints extensive configuration information about Perl. Can specify a specific configvar for targeted details.

-d
    Runs the Perl debugger.

-s
    Enables rudimentary parsing for command-line switches.

-x[directory]
    Extracts and executes a Perl script embedded within a larger text file (e.g., a shell script).

DESCRIPTION

Perl (Practical Extraction and Report Language) is a versatile, high-level programming language designed for efficient text manipulation, system administration, web development, and network programming. It uniquely combines features from scripting languages like sed and awk with capabilities found in C and shell scripting. Its core strength lies in its powerful regular expression engine, making it exceptionally adept at parsing, extracting, and transforming complex text data. Perl supports both procedural and object-oriented programming paradigms, offering flexibility for various project scales. It boasts a vast ecosystem of reusable modules available on CPAN, extending its functionality for almost any task. Executable directly from the command line or as standalone scripts, Perl remains a robust tool for quick prototyping and complex automation.

CAVEATS

Readability: Highly concise Perl code (often dubbed "write-only") can be challenging to understand or maintain, especially for beginners.
Performance: While generally fast for text processing, for computationally intensive tasks, Perl might be slower than compiled languages like C or Java.
Security: For scripts dealing with external input (e.g., web applications), understanding and utilizing taint mode (-T) is crucial to prevent security vulnerabilities.

CPAN (COMPREHENSIVE PERL ARCHIVE NETWORK)

A vast, publicly available repository of Perl modules and distributions. CPAN significantly extends Perl's capabilities, offering pre-built solutions for almost any programming task, from database connectivity to GUI development.

PERL ONE-LINERS

Perl's syntax and command-line switches (-e, -n, -p, -l, -i) make it exceptionally powerful for executing complex text processing tasks or system administration commands in a single line directly from the shell. These "one-liners" are a hallmark of Perl's utility for quick, efficient scripting.

HISTORY

Perl was created by Larry Wall in 1987 as a general-purpose Unix scripting language aimed at simplifying report processing. It quickly gained traction due to its powerful text processing capabilities, particularly its advanced regular expressions, and its ability to effortlessly interface with system calls and other Unix utilities. During the 1990s, Perl became a dominant language for web development, especially for CGI scripting. Although its primary role in web development has evolved with the rise of other frameworks and languages, Perl continues to be a robust tool for system administration, network programming, bioinformatics, and rapid prototyping.

SEE ALSO

sed(1), awk(1), grep(1), python(1), ruby(1)

Copied to clipboard