cheetsheetz/perl.md

281 lines
3.6 KiB
Markdown

# Perl Cheat Sheet
## Basics
### Comments
```perl
# This is a comment
```
### Print
```perl
print "Hello, World!\n";
```
### Variables
```perl
$scalar_variable = 42;
@array_variable = (1, 2, 3);
%hash_variable = ('key' => 'value');
```
## Control Structures
### if-else
```perl
if ($condition) {
# Code to execute if condition is true
} else {
# Code to execute if condition is false
}
```
### foreach
```perl
foreach $item (@array) {
# Code to execute for each item in the array
}
```
### while
```perl
while ($condition) {
# Code to execute while condition is true
}
```
## Subroutines
```perl
sub greet {
my $name = shift;
print "Hello, $name!\n";
}
greet("Alice");
```
## Regular Expressions
### Matching
```perl
if ($string =~ /pattern/) {
# Code to execute if pattern is found in $string
}
```
### Substitution
```perl
$string =~ s/find/replace/;
```
## File Handling
### Open and Read
```perl
open(my $file, '<', 'filename.txt') or die "Can't open file: $!";
while (<$file>) {
chomp;
print "$_\n";
}
close($file);
```
### Open and Write
```perl
open(my $file, '>', 'output.txt') or die "Can't open file: $!";
print $file "Hello, File!\n";
close($file);
```
## Modules
### Using Modules
```perl
use Module::Name;
```
### Exporting Functions
```perl
use Module::Name qw(function1 function2);
```
## References
### Scalar Reference
```perl
$scalar_ref = \$scalar_variable;
```
### Array Reference
```perl
$array_ref = \@array_variable;
```
### Hash Reference
```perl
$hash_ref = \%hash_variable;
```
# Advanced Perl Cheat Sheet
## Basics
### Comments
```perl
# This is a comment
```
### Print
```perl
print "Hello, World!\n";
```
### Variables
```perl
$scalar_variable = 42;
@array_variable = (1, 2, 3);
%hash_variable = ('key' => 'value');
```
### Data Types
```perl
$string = "Perl";
$number = 42;
$float = 3.14;
```
## Control Structures
### if-else
```perl
if ($condition) {
# Code to execute if condition is true
} elsif ($another_condition) {
# Code to execute if another condition is true
} else {
# Code to execute if all conditions are false
}
```
### foreach
```perl
foreach $item (@array) {
# Code to execute for each item in the array
}
```
### while
```perl
while ($condition) {
# Code to execute while condition is true
}
```
## Subroutines
```perl
sub greet {
my $name = shift;
print "Hello, $name!\n";
}
greet("Alice");
```
### Returning Values
```perl
sub add {
my ($num1, $num2) = @_;
return $num1 + $num2;
}
my $result = add(3, 4);
print "Sum: $result\n";
```
## Regular Expressions
### Matching
```perl
if ($string =~ /pattern/) {
# Code to execute if pattern is found in $string
}
```
### Substitution
```perl
$string =~ s/find/replace/;
```
## File Handling
### Open and Read
```perl
open(my $file, '<', 'filename.txt') or die "Can't open file: $!";
while (<$file>) {
chomp;
print "$_\n";
}
close($file);
```
### Open and Write
```perl
open(my $file, '>', 'output.txt') or die "Can't open file: $!";
print $file "Hello, File!\n";
close($file);
```
## Modules
### Using Modules
```perl
use Module::Name;
```
### Exporting Functions
```perl
use Module::Name qw(function1 function2);
```
## References
### Scalar Reference
```perl
$scalar_ref = \$scalar_variable;
```
### Array Reference
```perl
$array_ref = \@array_variable;
```
### Hash Reference
```perl
$hash_ref = \%hash_variable;
```
## Structuring a Perl Script for Bash
### Create a Perl Script
```perl
#!/usr/bin/perl
use strict;
use warnings;
# Perl script code here
print "Hello from Perl!\n";
```
### Make it Executable
```bash
chmod +x script.pl
```
### Run from Bash
```bash
./script.pl
```