Perl 是一种脚本语言,它内置了正则表达式,在文本和字符串处理方面有一定的优势。
Perl one liners
Perl doc intro
运行
1 2 3 4 5 6 7 8
| perl prog.pl
use strict; use warning;
|
语法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
my $var = 123;
my @arr = ("camel", 42, 1.23); $arr[0]; $arr[ @arr
@arr[0,1]; @arr[0..2]; my @sorted_arr = sort @arr; my @reversed_arr = reverse @arr;
my %fruit_color = ( apple => "red", banana => "yellow", ); $fruit_color{"apple"}; my fruits = keys %fruit_color; my colors = values @fruit_color;
if ($var) { ... } elsif ($var eq 'bar') { ... } else { ... }
unless (condition) { ... }
print "123" if $var print "345" unless $var
while (condition) { ... }
until (condition) { ... }
for ($i = 0; $i <= 5; $i++) { ... }
foreach (@array) { ... }
$_ foreach (1..5) { print "$_\n"; }
sub subroutine {
... }
= . x .. <=> q{} qq{} qx{}
if ($a =~ /regex/) { ... } if ($email =~ /([^@]+)@(.+)/) { print "Username is $1\n"; print "Hostname is $2\n"; }
|