Perl Cheat Sheet
Hello World:
print "Hello, World!\n";
Variables:
# Declaring variables
my $name;
my $age;
# Assigning values
$name = "John Smith";
$age = 25;
# Initializing variables
my $count = 0;
my $isTrue = 1;
Data Types:
# Scalar data types
my $number = 10;
my $floatingPoint = 3.14159;
my $string = "Hello";
my $boolean = 1;
# Array data type
my @array = (1, 2, 3, 4, 5);
# Hash data type
my %hash = (
key1 => "value1",
key2 => "value2"
);
Operators:
# Arithmetic operators
my $sum = 5 + 3;
my $difference = 10 - 4;
my $division = 15 / 2;
my $remainder = 15 % 2;
my $product = 4 * 6;
# Comparison operators
my $isEqual = (5 == 5);
my $isGreater = (10 > 5);
my $isLessThanOrEqual = (15 <= 20);
# Logical operators
my $logicalAnd = 1 && 0;
my $logicalOr = 1 || 0;
my $logicalNot = !1;
Conditional Statements:
# If statement
if (condition) {
# Code to execute if the condition is true
} else {
# Code to execute if the condition is false
}
# Unless statement
unless (condition) {
# Code to execute if the condition is false
}
# Switch statement
my $day = 3;
given ($day) {
when (1) {
print "Monday\n";
}
when (2) {
print "Tuesday\n";
}
when (3) {
print "Wednesday\n";
}
default {
print "Invalid day\n";
}
}
Loops:
# While loop
while (condition) {
# Code to execute while the condition is true
}
# Do-while loop
do {
# Code to execute
} while (condition);
# For loop
for (my $i = 0; $i < 10; $i++) {
# Code to execute repeatedly until the condition becomes false
}
# Foreach loop
foreach my $element (@array) {
print "$element\n";
}
Subroutines:
# Subroutine declaration
sub functionName {
# Code to execute
}
# Subroutine with parameters
sub multiply {
my ($a, $b) = @_;
my $result = $a * $b;
return $result;
}
# Subroutine call
my $result = functionName(arguments);
Regular Expressions:
# Matching
if ($string =~ /pattern/) {
# Code to execute if the pattern matches
}
# Substitution
$string =~ s/pattern/replacement/;
# Splitting
my @parts = split /delimiter/, $string;
# Joining
my $newString = join "delimiter", @array;