perl

developmentLinux/Unix
The perl command is one of the most frequently used commands in Linux/Unix-like operating systems. perl The Perl 5 language interpreter

Quick Reference

Command Name:

perl

Category:

development

Platform:

Linux/Unix

Basic Usage:

perl [options] [arguments]

Common Use Cases

    Syntax

    perl [options] [program_file] [arguments]

    Options

    Option Description
    -0[octal/hex] Specify record separator ($/) as octal/hexadecimal
    -a Autosplit mode with -n or -p (splits $_ into @F)
    -C[number/list] Control Unicode features
    -c Check syntax only, without executing
    -d[:debugger] Run program under debugger
    -D[number/list] Set debugging flags
    -e 'command' Execute one line of program
    -E 'command' Execute one line of program with all features enabled
    -f Don't do $sitelib/sitecustomize.pl
    -F/pattern/ Split on pattern with -a (instead of whitespace)
    -i[extension] Edit files in place (make backup with extension)
    -Idirectory Add directory to include path (@INC)
    -l[octal] Enable line ending processing, specifying output record separator
    -m[-]module Execute 'use/no module' before executing program
    -M[-]module Execute 'use/no module' at beginning of program
    -n Loop around reading each line, not implying 'print'
    -p Loop around reading each line, with 'print' at end of loop
    -s Enable rudimentary switch parsing for switches after programfile
    -S Look for program in PATH environment variable
    -t Enable tainting warnings
    -T Enable tainting checks
    -u Dump core after compiling
    -U Allow unsafe operations
    -v Print version, patchlevel and license information
    -V[:variable] Print configuration information or specific variable
    -w Enable many useful warnings
    -W Enable all warnings
    -x[directory] Extract Perl program from input (optionally CD to directory)
    -X Disable all warnings

    Examples

    How to Use These Examples

    The examples below show common ways to use the perl command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    perl -e 'print "Hello, World!\n"'
    Execute a Perl one-liner to print text.
    perl script.pl
    Run a Perl script file.
    # Advanced Examples Advanced
    perl -p -i.bak -e 's/foo/bar/g' file.txt Replace all occurrences of "foo" with "bar" in file.txt, creating a backup. perl -n -e 'print if /pattern/' file.txt Print lines matching a pattern from file.txt. perl -MModule::Name -e 'code' Use a specific module in a Perl one-liner. perl -d script.pl Run a script in debug mode. perl -c script.pl Check syntax without executing the script. perl -v Display Perl version information. perl -E 'say "Hello, World!"' Use modern features with the -E flag. perl -wnl -e '/pattern/ and print "$ARGV:$.: $_"' * Search for a pattern in multiple files, showing filename and line number.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    Perl is a high-level, general-purpose, interpreted, dynamic programming language that was originally developed by Larry Wall in 1987 as a Unix scripting language for text manipulation. Over the decades, it has evolved into a powerful programming language with a rich ecosystem, extensive libraries, and flexible syntax that combines elements from shell scripting, C, sed, awk, and other languages. The name "Perl" originally stood for "Practical Extraction and Report Language," though it's now often interpreted as "Pathologically Eclectic Rubbish Lister" in a self-deprecating manner embraced by the Perl community. The language is known for its mantra "There's More Than One Way To Do It" (TMTOWTDI, pronounced "tim-toady"), which reflects its flexible approach to problem-solving. Key features of Perl include: 1. Text Processing Prowess: Perl excels at text manipulation with powerful regular expression capabilities built directly into the language syntax, making it ideal for parsing, transforming, and generating text data. 2. Pragmatic Design: Perl was designed to be practical rather than beautiful, focusing on getting tasks done efficiently. It borrows useful features from many other languages without adhering to a single programming paradigm. 3. CPAN (Comprehensive Perl Archive Network): One of Perl's greatest strengths is its vast repository of reusable modules that extend the language's capabilities. CPAN contains over 250,000 modules covering virtually every programming need. 4. Versatility: Perl supports multiple programming paradigms including procedural, object-oriented, and functional programming styles. 5. Embedded Documentation: Perl introduced the concept of embedded documentation with its POD (Plain Old Documentation) format, allowing code and documentation to coexist in the same file. 6. One-Liners: Perl's concise syntax and command-line options make it excellent for powerful one-liners that can replace complex shell scripts. 7. Cross-Platform Compatibility: Perl runs on over 100 platforms, from mainframes to portable devices, ensuring scripts work consistently across different operating systems. Common use cases for Perl include: - System administration and automation - Web development (with frameworks like Catalyst, Mojolicious, and Dancer) - Data extraction, transformation, and loading (ETL) - Bioinformatics and genomic research - Network programming and socket communications - Report generation and data analysis - Legacy system integration and maintenance - Quick prototyping and proof-of-concept development While newer languages have emerged for some of these tasks, Perl continues to be a valuable tool in the programmer's toolkit, particularly for text processing, system administration, and maintaining legacy systems. Its expressiveness, flexibility, and vast library ecosystem make it a practical choice for many real-world problems. Perl 5, the current major version, has maintained remarkable stability and backward compatibility while continuing to evolve. Meanwhile, Perl 6 (now renamed to Raku) has developed as a separate language inspired by Perl but with significant differences in design and implementation. The perl command invokes the Perl interpreter, which can execute Perl scripts directly or run Perl code provided on the command line. Its numerous options provide flexibility for debugging, syntax checking, module inclusion, and various runtime behaviors that make Perl a versatile tool for both quick scripts and complex applications.

    Related Commands

    These commands are frequently used alongside perl or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the perl command works in different scenarios.

    $ perl
    View All Commands