95 lines
3.1 KiB
Markdown
95 lines
3.1 KiB
Markdown
# Perl
|
|
Perl is a scripting [programming language](programming_language.md) created by Larry Wall in 1987. Originally meant
|
|
for text processing (as a replacement for [AWK](awk.md)) it latter became a general purpose language. It is quite
|
|
[bloated](bloat.md) and has only a single relevant implementation (written in C). Also it is known for its syntax,
|
|
which can be considered as "inelegant" and leading to "write-only" programs, also the extensive use of sigils;
|
|
marks used before identifiers used as a way of communicating the data type of the underlying identifiers; using
|
|
the wrong sigil can lead to unexpected type conversion.
|
|
|
|
Perl includes powerful [regex](regular_expression.md) capabilities, although its regex engine is susceptible to catastrophic
|
|
backtracking.
|
|
|
|
It is shipped by default on most [Linux](linux.md) distributions, so it could be used as a replacement for [shell scripts](shell_script.md)
|
|
in some applications, but writing such a program in a more efficient language such as [C](c.md) is preferred.
|
|
|
|
## Notes
|
|
### Stricter language and warnings
|
|
This two lines meant to be put at the top of a script serve as boilerplate to make Perl more strict and saner:
|
|
```perl
|
|
use warnings;
|
|
use strict;
|
|
```
|
|
|
|
### Sigils
|
|
- `$`: scalar value; numbers, strings...
|
|
- `@`: [array](array.md)
|
|
- `%`: [hash table](hash_table.md)
|
|
- `&`: functions; rare in "modern" Perl code.
|
|
|
|
### Feature toggling
|
|
For its credit, Perl has a decent level of backwards compatibility compared to other scripting
|
|
languages, so some features that may cause problems with older code are disabled by default. Perl
|
|
allows enabling a set of features manually, for example here we enable the `say` function:
|
|
```perl
|
|
use feature 'say'; # this can also be an array of features...
|
|
```
|
|
More conveniently, instead of enabling each feature manually one can simply pick a certain version
|
|
and all of its features will be enabled:
|
|
```perl
|
|
use v5.34; # enables `say`, `defer`, `try`-`catch` and more...
|
|
```
|
|
|
|
### Automatically die on any error
|
|
This makes a Perl script commit suicide if any error is raised (akin to shell script `set -e`):
|
|
```perl
|
|
use autodie;
|
|
```
|
|
|
|
## Miscellaneous weirdness
|
|
- When calling the Perl interpreter directly, any [shebang](shebang.md) at the top of the script that doesn't actually call
|
|
the Perl interpreter itself gets executed, just like the kernel would interpret the shebang.
|
|
- ...
|
|
|
|
## Examples
|
|
### Hello world
|
|
```perl
|
|
# with the classical `print`:
|
|
print "Hello, World!\n";
|
|
# with `say`:
|
|
say "Hello, World!";
|
|
```
|
|
|
|
### Factorial
|
|
```perl
|
|
use warnings;
|
|
use strict;
|
|
use bignum; # optional
|
|
|
|
sub fact {
|
|
my $n = shift;
|
|
my $res = 1;
|
|
while ($n > 0) {
|
|
$res *= $n;
|
|
--$n;
|
|
}
|
|
return $res;
|
|
}
|
|
|
|
my @ns = (5, 0, 39, 1, 4, 10);
|
|
foreach my $n (@ns) {
|
|
printf "fact(%d) = %s\n", $n, fact($n);
|
|
}
|
|
```
|
|
|
|
### As a replacement for `sed`
|
|
It is possible to use Perl as `sed` to benefit from its more powerful regex:
|
|
```sh
|
|
# you can also pass `-i` to do inline replacement, like in sed
|
|
perl -p -e 's/foo/bar/g' files...
|
|
```
|
|
|
|
TODO
|
|
|
|
## Resources
|
|
- [Perl official website](https://www.perl.org/)
|
|
- [Perl Maven](https://perlmaven.com/): page containing tutorials and more
|