Getting Started
5 snippetsBasic Perl syntax and first programs
Hello World
print "Hello, World!\n";Shebang
#!/usr/bin/perl
use strict;
use warnings;Comments
# Single line comment
=begin
Multi-line comment
=cutPrint vs Say
print "No newline";
say "With newline"; # Requires use 5.010Execute Perl
perl script.pl
perl -e 'print "code\n"' # One-linerVariables & Data Types
7 snippetsScalars, arrays, hashes, and references
Scalar Variables
my $name = "John";
my $age = 30;
my $price = 19.99;Arrays
my @fruits = ("apple", "banana", "cherry");
my @numbers = (1, 2, 3, 4, 5);Hashes
my %person = (
name => "John",
age => 30,
city => "NYC"
);Array Access
$fruits[0]; # "apple"
$fruits[-1]; # Last element
scalar(@fruits); # LengthHash Access
$person{name}; # "John"
keys %person; # ("name", "age", "city")
values %person; # ValuesReferences
my $arrayref = \@fruits;
my $hashref = \%person;
$arrayref->[0]; # Access
$hashref->{name}; # AccessUndef
my $value = undef;
if (!defined($value)) { print "undef\n"; }Operators
6 snippetsArithmetic, comparison, and logical operations
Arithmetic
$a + $b; # Addition
$a - $b; # Subtraction
$a * $b; # Multiplication
$a / $b; # Division
$a % $b; # Modulus
$a ** $b; # ExponentString Operations
$str . $str2; # Concatenation
$str x 3; # Repeat 3 times
$str eq $str2; # String equal
$str ne $str2; # String not equalNumeric Comparison
$a == $b; # Equal
$a != $b; # Not equal
$a > $b; # Greater
$a < $b; # Less
$a >= $b; # Greater or equal
$a <= $b; # Less or equalString Comparison
$a eq $b; # Equal
$a ne $b; # Not equal
$a gt $b; # Greater
$a lt $b; # Less
$a ge $b; # Greater or equal
$a le $b; # Less or equalLogical Operators
$a && $b; # AND
$a || $b; # OR
!$a; # NOT
$a and $b; # Low precedence AND
$a or $b; # Low precedence ORRange Operator
1..10; # (1,2,3,4,5,6,7,8,9,10)
'a'..'e'; # ('a','b','c','d','e')Tired of looking up syntax?
DocuWriter.ai generates documentation and explains code using AI.
Conditionals
4 snippetsMaking decisions with if, unless, and ternary
If/Elsif/Else
if ($age > 18) {
print "Adult";
} elsif ($age > 12) {
print "Teen";
} else {
print "Child";
}Unless
unless ($logged_in) {
print "Please login";
}Ternary Operator
my $status = $age >= 18 ? "Adult" : "Minor";Postfix If
print "Yes" if $condition;
die "Error" unless $ok;Loops
6 snippetsIterating with for, foreach, while, and until
For Loop
for (my $i = 0; $i < 10; $i++) {
print "$i\n";
}Foreach Loop
foreach my $fruit (@fruits) {
print "$fruit\n";
}While Loop
while ($count < 10) {
$count++;
}Until Loop
until ($done) {
# code
}Loop Controls
last; # Break
next; # Continue
redo; # Restart iterationPostfix Loops
print "$_\n" for @array;
print "$_\n" while <STDIN>;Subroutines
5 snippetsDefining and calling subroutines
Define Subroutine
sub greet {
my ($name) = @_;
return "Hello, $name!";
}Call Subroutine
my $msg = greet("John");
greet("John"); # Without parensMultiple Parameters
sub add {
my ($a, $b) = @_;
return $a + $b;
}Default Parameters
sub greet {
my $name = shift // "Guest";
return "Hello, $name!";
}Reference Parameters
sub modify {
my $arrayref = shift;
push @$arrayref, "new";
}Regular Expressions
5 snippetsPattern matching and text manipulation
Match
if ($str =~ /pattern/) {
print "Found!";
}Substitution
$str =~ s/old/new/; # First match
$str =~ s/old/new/g; # Global (all)Capture Groups
if ($str =~ /(\d+)-(\d+)/) {
print "First: $1, Second: $2";
}Modifiers
/pattern/i; # Case insensitive
/pattern/g; # Global
/pattern/m; # Multi-line
/pattern/s; # Single-line (. matches \n)Split
my @parts = split(/,/, $str);
my @words = split(/\s+/, $text);File I/O
5 snippetsReading and writing files
Open File for Reading
open(my $fh, '<', 'file.txt') or die "Cannot open: $!";
while (my $line = <$fh>) {
print $line;
}
close($fh);Open File for Writing
open(my $fh, '>', 'output.txt') or die $!;
print $fh "Hello\n";
close($fh);Append to File
open(my $fh, '>>', 'file.txt') or die $!;
print $fh "Appended\n";
close($fh);Read Entire File
open(my $fh, '<', 'file.txt') or die $!;
my $content = do { local $/; <$fh> };
close($fh);Check File Exists
if (-e 'file.txt') { print "Exists"; }
if (-f 'file.txt') { print "Is file"; }
if (-d 'dir') { print "Is directory"; }