Perl Tutorial

Text Processing

Parsing logs, manipulating strings, and handling data files

The Power of the Diamond Operator (<>)

In Perl, the <> operator allows you to read lines from files provided as command-line arguments, or from standard input if no files are given.
perl
while (<>) {
    print "Processing line: $_";
}
Wait! Combined with $_ and RegEx, this allows you to build a powerful text processing tool in just 3 lines of code.

Splitting and Joining

  • split: Breaks a string into an array based on a delimiter.
  • join: Combines an array into a single string.
perl
my $line = "root:x:0:0:root:/root:/bin/bash";
my @parts = split(/:/, $line);
print "User: $parts[0]\n";

Slurping vs. Line-by-Line

  1. Line-by-Line: Efficient for massive files. Only one line is in memory at a time.
  2. Slurping: Reading the entire file into a single string. Good for small files or multi-line RegEx.

chomp

Wait! When reading lines from a file, they include the newline character (\n) at the end. Always use chomp to remove it.
perl
while (<$fh>) {
    chomp; # Removes \n from $_
    # ... process ...
}

Summary

TermDescription
<>The diamond operator for reading files
$_The default variable (current line)
splitString to Array
joinArray to String
chompRemoving the trailing newline
indexFinding the position of a substring
substrExtracting or replacing a portion of a string
Best ForETL (Extract, Transform, Load) tasks and log analysis
Logic"Practical data manipulation"
SafetyMemory efficient when processing line-by-line
ModernStill used by sysadmins and data scientists daily
StandardUse autodie for better file error handling
IdentityThe "P" in the old "LAMP" stack (Linux, Apache, MySQL, Perl)