寒假【perl语言学习脚本】17.1 17.2 17.3

寒假【perl语言学习脚本】17.1 17.2 17.3
#!/usr/bin/perl -w
my $filename =’/home/zhangjingjing/perl5/sample_text.txt’;
open my fh,<,fh,'<',filename or die “can’t open 'filename:filename':!”;
chomp (my $strings =);
while(1){
print ‘please enter a pattern:’;
chomp (my $pattern=);
last if KaTeX parse error: Expected 'EOF', got '\A' at position 12: pattern=~ /\̲A̲\s*\Z/; my @mat…pattern/,@strings;
};
if(KaTeX parse error: Expected '}', got 'EOF' at end of input: …{ print "error:@";
}else{
my [email protected];
print "there were KaTeX parse error: Expected 'EOF', got '\n' at position 25: …ching strings :\̲n̲", map "\n",@matches;
}
print “\n”;
}
#这个程序使用eval块来捕获使用正则表达式的时候可能发生的错误
#在eval块里,grep会筛选出字符串列表中匹配模式的字符串
#一旦eval执行完毕,程序就会汇报错误信息或显示匹配的字符串
#从文件中读取一组字符串(每行一个),然后让用户键入模式以便进行字符串匹配
#对每一个模式,程序应该说明文件里共有多少字符串匹配成功
#分别是哪些字符串
#对于所键入的每个新模式,不应该重新读取文件,应该把这些字符串存放在内存里
#使用sample_txt这个文件
寒假【perl语言学习脚本】17.1 17.2 17.3
#!/usr/bin/perl -w
foreach(glob(’*’)){
my(atime,atime,mtime)=(stat)[8,9];
printf “%-20s %10d %10d\n”,,_,atime,$mtime;
}
#报告当前目录下所有文件的最后访问时间和最后修改时间,单位为秒,用stat取得文件的时间戳信息
#利用列表切片的写法提取这两个元素
#查阅stat的文档,位于第8和第9索引位置上的就是我们需要的时间戳
寒假【perl语言学习脚本】17.1 17.2 17.3
#!/usr/bin/perl -w
foreach my KaTeX parse error: Expected '}', got 'EOF' at end of input: …glob('*')){ my(atime,KaTeX parse error: Expected '}', got 'EOF' at end of input: mtime)=map{ my(year,month,month,day)=(localtime($
))[5,4,3];
year+=1900;year+=1900;month+=1;
sprintf ‘%4d-%02d-%02d’,year,year,month,$day;
}(stat file)[8,9];printf"file)[8,9]; printf "%-20s %10s %10s\n",file,atime,atime,mtime;
}
#解题关键是用localtime把纪元时间转换成YYYY-MM-DD这样的日期格式
#假设时间戳放在KaTeX parse error: Expected 'EOF', got '#' at position 17: …里面(它是map的控制变量) #̲我们从localtime文档查…year,month,month,day)=(localtime)[5,4,3];
#localtime返回的年份数字需要另外加上1900,月份数字需要另外加上1,才符合我们平时的习惯
#所以做调整:year+=1900;year+=1900;month+=1;
#最后,再把这些数据组合成指定的格式,并对单个数字的月和日部分添上前置零

sprintf ‘%4d-%02d-%02d’,year,year,month,$day;