articles/perl.md: add factorial example
This commit is contained in:
parent
bb73a074eb
commit
f0d96e468d
1 changed files with 22 additions and 0 deletions
|
@ -59,6 +59,28 @@ print "Hello, World!\n";
|
|||
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
|
||||
|
|
Loading…
Add table
Reference in a new issue