1.管道:在perl中使用管道时,管道用于把系统命令的输出作为perl的输入,或者将perl的输出作为系统命令的输入。管道又称为过滤器(filter)。用户必须通过open系统调用来使用管道。该系统调用接受两个参数:一个是用户自定义的句柄,另一个是操作系统命令,并需要在操作系统命令的前面或者后面加上一个“|”符号。如果“|”符号出现在系统命令之前,那么表示该命令将把perl的输出内容作为命令的输入内容,否则表示perl将会读取系统命令的输出作为自身的输入内容。
[root@dou shili]# cat 1.pl
#!/usr/bin/perl -w
use strict;
open(my $fh,"ls |") or die "cannt perform command ls:$!";
while(<$fh>) {print;};
close $fh;
print "-" x 5 . "n";
open(my $f,"| sort") or die "cant get perl command's output:$!";
print $f "dogsncatsnbirdsn";
close $f;
[root@dou shili]# ls
1.pl? file? reandw.pl
[root@dou shili]# perl 1.pl
1.pl
file
reandw.pl
-----
birds
cats
dogs
?
2.一些正则表达式元字符:
^:匹配行首
$:匹配行尾
a.c: 匹配一个a,后面任意一个字符,然后后面是一个c字符
[abc]:匹配a或者b或者c
[^abc]:不匹配abc的其他任何字符
[0-9]:匹配0-9之间的任意一个数字
ab*c:匹配a然后加上任意多个b然后后面是c的情况
ab+c:匹配a后面跟大于等于1个b最后是一个c的情况
ab?c:匹配a后面跟0个或者1个b最后是一个c的情况
(ab)+c:匹配大于等于一个ab后面跟一个c的情况
(ab)(c):匹配ab并将其赋值与变量$1,同时捕获c并赋予变量$2.
3.特殊的文件句柄ARGV
perl通过@ARGV数组保存了命令行提供的参数内容。如果用到了ARGV文件句柄,则这些命令行参数将被视为文件;否则视为命令行字符串。
[root@dou shili]# cat 1
444
hello
[root@dou shili]# cat file
1
Hello World
[root@dou shili]# cat 3.pl
#!/usr/bin/perl -w
use strict;
print "@ARGVn";
while(<ARGV>) {
??????? print;
}
[root@dou shili]# perl 3.pl 1 file
1 file
444
hello
1
Hello World
?
4.引用reference
perl的引用本质上是一个标量型变量,负责保存另一个变量的地址。在创建引用时,需要使用反斜杠符号。
[root@dou shili]# cat 4.pl
#!/usr/bin/perl -w
use strict;
#create variables
my $age = 18;
my @array = qw(hello world come on);
my %home = ( owner => "me",
??????????????? benben => "dell",
??????????????? shuihu => "mingpai",
);
#create references
my $pointer1 = $age;
my $pointer2 = @array;
my $pointer3 = %home;
my $pointer4 = [ qw(black white red yellow) ];
my $pointer5 = { country => "china",province => "shandong",};
#dereference
print "$$pointer1n";
print "@$pointer2n";
print "%$pointer3n";
print "$pointer3->{benben}n";
print "$pointer4->[2]n";
print "$pointer5->{country}n";
[root@dou shili]# perl 4.pl
18
hello world come on
%HASH(0x1a54d720)
dell
red
china
?
5.对象:perl中的对象是一种特殊类型的变量。在perl中,一个类代表着含有一组变量(属性)与函数(方法)的一个包。perl没有提供专门的关键字class。其中,属性就是能够描述这个对象的变量;而方法则是一种特殊的函数,允许用户创建并操作给定的对象。在创建对象时,用户需使用bless函数。
[root@dou shili]# cat 5.pl
#!/usr/bin/perl -w
use strict;
# create a class
package pet;
sub new { #constructor
??????? my $class = shift;
??????? my $pet = {
??????????????? name => "dog",
??????????????? owner => "me",
??????????????? type => "guochan",
??????? };
??????? bless($pet,$class);
??????? # return a pointer to the object
??????? sub set_pet { # accessor methods
??????????????? my $self = shift;
??????????????? my ($name,$owner,$type) = @_;
??????????????? $self->{'name'} = $name;
??????????????? $self->{'owner'} = $owner;
??????????????? $self->{'type'} = $type;
??????? }
??????? sub get_pet {
??????????????? my $self = shift;
??????????????? while(my ($key,$value) = each(%$self)) {
??????????????????????? print "$key: $valuen";
??????????????? }
??????? }
}
# instantiation a class my $cat = pet->new(); #create an object with a constructor method $cat->set_pet("dahuanggou","tom","miguochan"); #access the object with an instance $cat->get_pet; [root@dou shili]# perl 5.pl owner: tom name: dahuanggou type: miguochan ?