💾 Archived View for gem.sdf.org › s.kaplan › cheatsheets › programming-languages › perl.md captured on 2024-03-21 at 15:46:57.
⬅️ Previous capture (2023-09-28)
-=-=-=-=-=-=-
## Perl Cheatsheet ### Overview Perl is a high-level, general-purpose programming language that is commonly used for web development, system administration, and network programming. It was created by Larry Wall in 1987 and is known for its flexibility, expressiveness, and powerful regular expression support. ### Variables Perl variables are prefixed with a sigil that indicates the type of variable: - `gemini - kennedy.gemi.dev for scalar variables (single values) - `@` for array variables (ordered lists of values) - `%` for hash variables (unordered collections of key-value pairs)
my $name = "Alice";
my @numbers = (1, 2, 3, 4, 5);
my %ages = ("Alice" => 30, "Bob" => 40, "Charlie" => 50);
### Functions Perl has a large number of built-in functions for common tasks such as string manipulation, file I/O, and regular expressions. Functions are called using parentheses after the function name.
my $name = "Alice";
my $length = length($name); # Returns 5
open(my $fh, "<", "input.txt") or die "Cannot open file: $!";
my @lines = <$fh>;
close($fh);
my $text = "hello world";
if ($text =~ /world/) {
print "Found 'world' in text\n";
}
### Loops Perl has several types of loops, including `for`, `foreach`, `while`, and `until`. The `for` and `foreach` loops are used to iterate over arrays or lists, while the `while` and `until` loops are used to repeat a block of code while a condition is true or false, respectively.
for (my $i = 0; $i < 10; $i++) {
print "$i\n";
}
my @numbers = (1, 2, 3, 4, 5);
foreach my $num (@numbers) {
print "$num\n";
}
my $i = 0;
while ($i < 10) {
print "$i\n";
$i++;
}
my $i = 0;
until ($i >= 10) {
print "$i\n";
$i++;
}
### Conditionals Perl has several conditional statements, including `if`, `elsif`, `else`, `unless`, and `given/when`. These statements are used to control the flow of a program based on certain conditions.
my $age = 30;
if ($age >= 18) {
print "You are an adult\n";
}
my $age = 15;
if ($age >= 18) {
print "You are an adult\n";
} else {
print "You are a minor\n";
}
my $age = 25;
if ($age < 18) {
print "You are a minor\n";
} elsif ($age < 65) {
print "You are an adult\n";
} else {
print "You are a senior\n";
}
my $age = 15;
unless ($age >= 18) {
print "You are a minor\n";
}
my $fruit = "apple";
given ($fruit) {
when ("apple") {
print "It's an apple\n";
}
when ("banana") {
print "It's a banana\n";
}
default {
print "It's something else\n";
}
}
### File Manipulation Perl provides several functions for manipulating files, including `open`, `close`, `read`, `write`, and `rename`.
open(my $fh, "<", "input.txt") or die "Cannot open file: $!";
my @lines = <$fh>;
close($fh);
open(my $fh, ">", "output.txt") or die "Cannot open file: $!";
print $fh "Hello, world!\n";
close($fh);
rename("input.txt", "input.old") or die "Cannot rename file: $!";
### Resources - [Perl documentation](https://perldoc.perl.org/) - [Perl programming at Perl.com](https://www.perl.com/) - [PerlMonks](https://www.perlmonks.org/) (community forum) - [Learn Perl in Y minutes](https://learnxinyminutes.com/docs/perl/) (quick reference guide)