数组定义: my @p1=('aaa','bbb','ccc'); my @p2=(1..100); my @p3=qw(aaa bbb ccc); my @p4=qw! aaa bbb ccc !; $end=$#p1; $number=$#p1+1; $last=$p1[$#p1]; $last=$p1[-1];
可以使用state操作符来声明持久性私有变量 state $sum=0; state @numbers;
打印数组: print @array print "@array"
@ARGV是存放所有参数的数组
<>砖石操作符,把输入参数当作文件处理
文件操作符(文件句柄) open CONFIG, 'test'; open CONFIG, ' open CONFIG, '>test'; open CONFIG, '>>test'; 关闭文件句柄: close CONFIG; 读取或者写入的时候指定文件的字符编码 open CONFIG, '<:encoding(UTF-8)', 'test'; open CONFIG, '>>:encoding(UTF-8)', 'test'; 打印perl能处理的字符编码清单 perl -MEncode -le "print for Encode->encodings(':all')"
hash 申明hash my %test=('foo'=>35,'bar'=>39,'carl'=>34,'zhang'=>89); my %test=('foo',35,'bar',39,'carl',34,'zhang',89); 访问哈希元素 $hash{$key}; 哈希元素赋值 $hash{'test'}='carlzhang'; 访问整个hash my @any_array=%array; print "@any_array\n"; hash转换 my %test_hash=reverse %hash; hash函数 keys函数能返回hash的所有键列表 values函数能返回hash所有的值列表 my %test=('foo'=>35,'bar'=>39,'carl'=>34,'zhang'=>89); my @test_key=keys%test; my @test_value=values%test; print "test_key is: @test_key\n"; print "test_value is: @test_value\n"; 使用each函数遍历hash my %test=('foo'=>35,'bar'=>39,'carl'=>34,'zhang'=>89); while( my($test_key,$test_value)=each %test){ print "$test_key=>$test_value\n"; }
my %test=('foo'=>35,'bar'=>39,'carl'=>34,'zhang'=>89); foreach my $test_key (sort keys %test){ my $test_value=$test{$test_key}; print "$test_key=>$test_value\n"; } 判断键值是否存在: if ( exists $test{'foo'}){ print "key fool is in test hash\n"; } 删除hash中的某个元素 delete $test{'foo'}; %ENV环境变量 my @test=%ENV; foreach $test_key (keys %ENV){ print "$test_key=>$ENV{$test_key}\n"; }
perl模块 perldoc CGI 查看模块的用法 cpan -a 列出安装的模块 模块的安装: perl Makefile.PL make make install 可以在perl Makefile.PL后面通过INSTALL_BASE指定安装的路径
File::Basename和File::Spec的使用 use File::Basename; use File::Spec; my $name="/usr/local/bin/perl"; my $filename=basename $name; my $dirname=dirname $name; my $new_name=File::Spec->catfile($dirname,$filename); print "filename is $filename\n"; print "dirname is $dirname\n"; print "newname is $new_name\n";
perl目录操作 chdir改变工作目录 chdir '/etc' or die "can't chdir to /etc: $!\n"; 在程序内部使用通配符匹配文件 my @files=glob '.* *'; #匹配多个模式需要用空格隔开,也可以采用my @files=<*>;的方式 print "@files\n"; opendir 打开目录 readdir 读取目录下的文件名 closedir 关闭打卡的目录句柄 测试代码: use File::Spec::Functions; my $dir='/etc'; opendir DIR,$dir or die "can't open /etc directory: $!\n"; foreach my $file (readdir DIR){ if ($file=~/^\./){ next; } $file=catfile($dir,$file); print "we found $file in /etc directory\n"; } closedir DIR;
unlink删除文件,返回成功删除文件的数目,unlink不能用来删除目录 rename重命名文件 测试代码: for my $file (glob '*.old'){ my $new_file=$file; $new_file=~s/old/new/; if (-z $new_file){ print "$new_file is exists ,please check it out\n"; }else{ rename $file,$new_file or warn "rename $file faild,please check it out\n"; print "rename $file success ,don't worry\n"; } }
创建和删除目录 mkdir 创建目录 my $dir='aaa'; my $permissions="0755"; mkdir 'aaa',oct($permissions) or die "can't create directory aaa: $!\n"; rmdir删除目录,每次只能删除一个目录,而且删除时目录必须为空,不然会导致失败!
chmod修改文件或者目录权限 chmod 0755,'test'; chown修改文件或者目录的属主或者属组,返回受影响的文件数量 测试代码: my $user=52; my $group=52; chown $user,$group,'bbb'; defined(my $user1=getpwnam 'puppet') or die "bad user: $!\n"; defined(my $group1=getpwnam 'puppet') or die 'bad group: $!\n'; chown $user1,$group1,'bbb.new'; utime修改文件的时间戳(最近的更改和访问时间) 测试代码: my $now=time; my $ago=$now-24*60*60; utime $now,$ago,'bbb';
字符串和排序 用index查找子字符串 my $string='hah aaa bbbcc aa ddd'; my $part=index($string,'aa'); print "$part\n";
排序 按照数字排序<=>; 按照字符排序cmp; my @some_numbers=qw{1 10 23 100 34 45}; my @some_chars=qw{aa cc bb dd dc bc}; sub by_number{ $a<=>$b; } sub by_char{ $a cmp $b; } my @test=sort by_number @some_numbers; my @test2=sort by_char @some_chars; print "@test\n"; print "@test2\n";
sub sort_test{ substr($test1{$a},2,6) <=> substr($test1{$b},2,6) or substr($test1{$a},0,1) cmp substr($test1{$b},0,1) }
执行外部命令 IPC::System::Simple system systemx capture capturex
错误扑捉: eval my $aaa1=$ARGV[0]; my $aaa2=$ARGV[1]; my $test=eval{$aaa1/$aaa2} || 'aaa'; print "I couldn't divide by \$aaa2: $@" if $@; print "$test"; 有4中类型的错误eval捕捉不到: 1、代码语法错误 2、perl解析器本省的崩溃错误 3、告警类错误 4、每次调用exit的时候