Fixed duplicate in Perl sheet.
This commit is contained in:
parent
608673cb2b
commit
4bc20c0ca3
119
perl.md
119
perl.md
|
|
@ -19,125 +19,6 @@ $scalar_variable = 42;
|
||||||
%hash_variable = ('key' => 'value');
|
%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
|
### Data Types
|
||||||
```perl
|
```perl
|
||||||
$string = "Perl";
|
$string = "Perl";
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue