寒假【perl语言学习脚本】12.1 12.2 12.3

在这里插入图片描述
#!/usr/bin/perl -w
foreach my $file(@ARGV){
my KaTeX parse error: Expected 'EOF', got '&' at position 12: attribs = &̲attributes(file);
print “’$file’ $attribs.\n”;
}
sub attributes{ #子程序
#报告某个给定文件的属性
my $file = shift @_;
return “does not exist” unless -e $file;
my @attrib;
push @attrib,“readable” if -r $file;
push @attrib,“writable” if -w $file;
push @attrib,“executable” if -x $file;
return “exists” unless @attrib;
'is ’ .join " and ",@attrib;#返回值
}

#在这个例子里,使用子程序
#对于每个文件,主循环会用一行来输出它的属性
#上面的子程序会告诉我们某个文件的属性,如果文件根本不存在,就不需要进行其他测试了,因此我们会先测试它是否存在,如果不存在,就提早返回
#如果文件确实存在,我们将会建立一个列表用来存储文件的属性
#要是取得了某些属性,我们可以用"and"把它们连接起来,使用join并且在前面加上"is",以造出is readable and writable这样的语句,
#这样的处理并不完美
#如果你碰巧没有在命令行键入任何文件名,则程序将不会有任何输出
#如果你查询零个文件的信息,本来就该得到零行的结果
在这里插入图片描述
#!/usr/bin/perl -w
die “no file names supplied!\n” unless @ARGV;
my $oldest_name =shift @ARGV;
my $oldest_age = -M $oldest_name;

foreach(@ARGV){
my a g e = − M ; ( age= -M; ( age=M;(oldest_name, o l d e s t a g e ) = ( oldest_age)=( oldestage)=(_,$age)
if a g e > age> age>oldest_age;
}
printf “the oldest file was %s, and it was %.1f days old.\n”, o l d e s t n a m e , oldest_name, oldestname,oldest_age;#必须是以逗号为间隔

#程序一开始就会检查文件名,如果没有取得任何文件名,就会显示错误信息
#再一次用到了高水线算法,第一个文件是目前唯一见过的文件中最旧的,我们必须记下它的年龄,并存储在KaTeX parse error: Expected 'EOF', got '#' at position 15: oldest_age变量里 #̲对于每个文件,我们都会像上面那…_的默认参数
#如果目前的文件年龄大于$oldest_age,我们就会以列表赋值的方式同时更新文件名和年龄变量的值

#此程序中,我们将-M返回的年龄存进临时变量$age里,如果不使用临时变量,每次都直接使用-M,又会怎样呢?
#除非使用特殊的_文件句柄,否则我们每次都得向操作系统询问文件的年龄,
#当我们第一次使用-M取得某个文件的年龄时,发现它是目前为止所看到的的最旧的
#程序结尾处以printf输出文件名和年龄,并将天数取到小数点后一位
在这里插入图片描述
#!/usr/bin/perl -w
use 5.010;
say “looking for my files that are readable and writable”;
die “no files specified!\n” unless @ARGV;
foreach my KaTeX parse error: Expected '}', got 'EOF' at end of input: …(@ARGV){ say "file is ours, is readable and writable" if -o -r -w $file;
}

#使用栈式文件测试符的前提是用perl 5.10及以上版本(用到了say),因此我们使用use语句开头来确保版本正确
#我们检查@ARGV数组,确保其中有数据供foreach处理,否则调用die
#我们使用三个文件测试操作符,-o检查我们是否拥有文件,-r检查文件是否可读,-w检查文件是否可写
#把它们堆叠在一起成为-o -r -w可以一并检查是否通过

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值