cheat.sheets:perl
# perl
# The Perl 5 language interpreter.
# Parse and execute a Perl script:
perl script.pl
# Check syntax errors on a Perl script:
perl -c script.pl
# Parse and execute a perl statement:
perl -e perl_statement
# Import module before execution of a perl statement:
perl -Mmodule -e perl_statement
# Run a Perl script in debug mode, using perldebug:
perl -d script.pl
# Loo[p] over all lines of a file, editing them [i]n-place
# using a find/replace [e]xpression
perl -p -i -e 's/find/replace/g' filename
# Run a find/replace expression on a file,
# saving the original file with a given extension:
perl -p -i'.old' -e 's/find/replace/g' filename
# See also:
# Perl language cheat sheets at /perl/
# list of pages: /perl/:list
# learn perl: /perl/:learn
# perl one-liners: /perl/1line
# search in pages: /perl/~keyword
# Perl method of listing out the environment variables, sans values.
perl -e 'print("$_\n") foreach keys(%ENV)'
# Output the columns and lines of the current terminal.
perl -e 'use Term::ReadKey "GetTerminalSize"; my ($Cols, $Lines) = GetTerminalSize(); print("${Cols}x$Lines\n")'
# List out all of the aliases within the provided file. This works by iterating
# over each line of the file, displaying only those lines matching the REGEX.
# Before printing out the relevant lines, all tabs are removed.
perl -ne '/^[[:space:]]+alias/ and print(tr/\t//dr)' "$HOME/.bash_aliases"
# Alternative logic approach:
perl -ne 'print(tr/\t//dr) if /^[[:space:]]+alias/' "$HOME/.bash_aliases"
# See if the current user has a non-empty password value. This may not work for
# systems set up with shadow passwords, however.
perl -ne '/^$ARGV[0]::$</ and print(STDERR "WARNING: User has an empty password.\n")' /etc/passwd ichy
# Display the current user's UID and GID in a format ideal for chmod(1).
perl -e 'print("$<:" . (split(" ", $)))[0] . "\n")'
# Permissions allowing, output the first 512 bytes of an MBR storage device, -
# where the partition table should be stored, using Perl's read() function.
#
# Note that the 2-argument style open() should not be used unless the file is
# static; no chance of changing to something potentially problematic.
#
# You might spot some printable strings, such as 'LILO' or 'GRUB', if you've
# also stored your bootloader on the MBR of the storage device (non-UEFI).
perl -e 'open(my $FH, "</dev/sda"); read($FH, my $Data, 512); close($FH); print("$Data\n")'
# Create a simple table of users within your system. This example demonstrates
# the use of the `getpwent()` subroutine, which, upon each execution, holds an
# array of each field in each line within the '/etc/passwd' file.
printf("%-15s %-5s %-5s %-s\n", 'USERNAME', 'UID', 'GID', 'HOME');
while (my @Data = getpwent()) {
printf("%-15s %-5s %-5s %-s\n", $Data[0], $Data[2], $Data[3], $Data[7])
}
cheat:perl
---
tags: [ perl ]
---
See https://perldoc.perl.org/perlrun
# View the perl version (long and short version):
perl -v
perl -V
# Run a program:
perl <program> [args]
# Syntax check a program:
perl -cw <program>
# Force warnings everywhere in the program:
perl -W <program>
# Add path1 to the module search path:
# The PERL5LIB env var does this too
perl -I <path1> <program> [args]
# Start the program in the perl debugger:
# See https://perldoc.perl.org/perldebug)
perl -d <program>
# Specify the program text as the argument to -e:
perl -e <program_text>
perl -e 'print "Hello World!\n"'
# Enable Unicode:
perl -C -e <program_text>
# Specify the program text and enable new features:
perl -E 'say "Hello World!"'
# Specify the program text and enable new features:
perl -M<module>[=import,list] -E <program_text>
# Compile then decompile a program with B::Deparse:
perl -MO=Deparse -E <program_text>
# Process files line-by-line (output on your own):
perl -ne <program_text> [files]
# Process files line-by-line (output $_ at each loop iteration):
perl -pe <program_text> [files]
# Read an entire file (or STDIN) into one big string.
# With v5.36 and later, -g is the same as -0777
perl -0777 -ne <program_text> [files]
perl -0777 -pe <program_text> [files]
perl -g -pe <program_text> [files]
# Split input lines on whitespace with -a, put into @F:
perl -ane <program_text>
# -a implies -n:
perl -ae <program_text>
# Splits lines on alternate separator with -F:
perl -aF<separator> -e <program_text>
# In-place editing with -p:
perl -pe <program_text> [files]
# In-place editing with -p and backup original with -i:
perl -pie <program_text> [files]
perl -pi.bak -e <program_text> [files]
# Replace string "\n" to newline:
echo -e "foo\nbar\nbaz" | perl -pe 's/\n/\\n/g;'
# Replace newline with multiple line to space:
cat test.txt | perl -0pe "s/test1\ntest2/test1 test2/m"
# Replace double newlines with single newline:
perl -pe '$/=""; s/(\n)+/$1/' my-file
tldr:perl
# perl
# The Perl 5 language interpreter.
# More information: <https://www.perl.org>.
# Parse and execute a Perl script:
perl script.pl
# Check syntax errors on a Perl script:
perl -c script.pl
# Parse and execute a Perl statement:
perl -e perl_statement
# Run a Perl script in debug mode, using `perldebug`:
perl -d script.pl
# Edit all file lines [i]n-place with a specific replacement [e]xpression, saving a backup with a new extension:
perl -p -i'.extension' -e 's/regular_expression/replacement/g' path/to/file
# Run a multi-line replacement [e]xpression on a file, and save the result in a specific file:
perl -p -e 's/foo\nbar/foobar/g' path/to/input_file > path/to/output_file
# Run a regular [e]xpression on `stdin`, printing matching [l]ines:
cat path/to/file | perl -n -l -e 'print if /regular_expression/'
# Run a regular [e]xpression on `stdin`, printing only the first capture group for each matching [l]ine:
cat path/to/file | perl -n -l -e 'print $1 if /before(regular_expression)after/'
$
cheat.sh