《Perl语言入门》第四版习题(11)

来源:互联网 发布:国际网络电话软件 编辑:程序博客网 时间:2024/05/19 00:17

1. [15]写一个程序,读入命令行中的一串文件,报告其是否可读,可写,可执行,或不存在。(提示:如果一个函数能一
次对一个文件进行所有的检测将非常有帮助。)如果一个文件被执行了chmod 0 操作,将报告什么?(在Unix 系统中,
chmod 0 some_file 将一个文件变成不可读,不可写,不可执行的)在大多数shell 中,星号(*)表示当前目录中的所
有的普通文件。也就是说,可以输入像./ex11-1 *这样的命令,返回当前目录下文件的属性。
2. [10]写一个程序,找出命令行中存在时间最长的文件名,并报告其天数。当参数为空时,其行为如何(例如,命令行
中没有输入任何的文件)?

 

1、

#!/usr/bin/perl -w
use strict;

sub fy{
        my $r;
        my $f=$_[0];
        #print $f;
        $r.='r' if -r $f;
        $r.='w' if -w $f;
        $r.='x' if -x $f;
        $r.='e' if -e $f;
        $r
}

for(@ARGV){
        print "$_:",fy($_),"/n";
}

 

2、

#!/usr/bin/perl -w
use strict;

die "No file names supplied!/n" unless @ARGV;
my $oldest_name = shift @ARGV;
my $oldest_age= -M $oldest_name;
foreach (@ARGV){
        my $age = -M;
        ($oldest_name,$oldest_age)=($_,$age) if $age>$oldest_age;
}
printf "The oldest file was %s, and it was %.1f days old./n",$oldest_name,$oldest_age;

 

原创粉丝点击