Perl问题汇总


1.perl正则如何忽略大小写?

其实就是模式匹配修饰…加在操作符后即可

在这里插入图片描述

2.如何在perl中调用shell?

2.1 system
  perl也可以用system调用shell的命令,它和awk的system一样,返回值也是它调用的命令的退出状态。如果向system传递一个字符串作参数,则perl会调用shell来执行这个命令,在这个字符串内也就不可以有perl的变量了;如果传递多个字符串作参数,则perl会自己执行这个命令,且可以传递perl自己的变量给它,因为perl会对这些变量扩展成它们的值。

[root@AX3sp2 ~]# cat aa.pl
#! /usr/bin/perl -w
$file = "wt.pl";
system("ls -l wt.pl");
$result = system "ls -l $file";
print "$result \n"; #输出命令的退出状态
system "date";
[root@AX3sp2 ~]# perl aa.pl
-rwxr-xr-x 1 root root 126 12-16 15:12 wt.pl
-rwxr-xr-x 1 root root 126 12-16 15:12 wt.pl
0
20101216日 星期四 15:58:34 CST     

2.2 反引号( ``)

perl的system函数和awk的一样不能够返回命令的输出。要得到命令的输出,就得使用和shell本身一样的命令。

[root@AX3sp2 ~]# cat bb.pl
#! /usr/bin/perl
print `date`;
print "this is test \n";
[root@AX3sp2 ~]# perl bb.pl
20101216日 星期四 15:51:59 CST
this is test

2.3 exec()
perl还可以使用exec来调用shell的命令. exec和system差不多,不同之处在于,调用exec之后,perl马上就退出,而不会去继续执行剩下的代码

[root@AX3sp2 ~]# cat cc.pl
#! /usr/bin/perl
exec ("echo this is test");
print "good bye !\n";  #这句话不会被输出
[root@AX3sp2 ~]# perl cc.pl
this is test

3.perl中列表、哈希的嵌套

详细参考链接 链接

3.1 hash嵌套hash

%group1 = (
 
'fruit' => 'banana',
 
'vegetable' => 'cauliflower',
 
'drink' => 'orange juice'
 
);
 
%group2 = (
 
'fruit' => 'apple',
 
'vegetable' => 'lettuce',
 
'drink' => 'apple juice'
 
);
 
%hash = (
 
'group1' => {%group1},
 
'group2' => {%group2},
 );

访问的时候使用:

$hash{'group1'}{'fruit'} = 'banana' and  $hash{group1}{fruit} are the same.

3.2 hash嵌套数组

@fruit = ("banana","apple","orange","pear")@vegetable = ("cauliflower","lettuce","tomato","cucumber")@drink = ("orange juice","apple juice","red tea","red wine")%hash = (
 
'fruit', [@fruit],
 
'vegetable', [@vegetable],
 
'drink', [@vegetable]
 
);

访问的时候使用:

$hash{'fruit'}[0] = 'banana';
 
$hash{'fruit'} = ['banana','apple','orange','pear'];

【参考】

1-Perl 正则表达式-https://www.runoob.com/perl/perl-regular-expressions.html
2-Perl调用shell指令-http://blog.sina.com.cn/s/blog_3ef3b17a0100dzvx.html
3-perl调用shell命令-https://www.jb51.net/article/49980.htm

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值