2
0
mirror of https://github.com/chubin/cheat.sheets synced 2024-11-15 06:12:59 +00:00
cheat.sheets/sheets/perl

61 lines
2.4 KiB
Plaintext
Raw Normal View History

2017-06-03 21:43:36 +00:00
# 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:
2017-06-07 12:55:33 +00:00
# Perl language cheat sheets at /perl/
2017-06-03 21:43:36 +00:00
# list of pages: /perl/:list
# learn perl: /perl/:learn
2017-06-07 12:55:33 +00:00
# perl one-liners: /perl/1line
2017-06-03 21:43:36 +00:00
# search in pages: /perl/~keyword
2020-02-02 19:44:01 +00:00
# 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")'
2020-02-17 01:27:30 +00:00
# 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")'
2020-11-09 19:35:18 +00:00
# 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")'