ԱŮװ
Ů
׷
Դװ
˼Դ
Ҿ̳
Ա
װ
װ
83ʱŮ
Ҿװ
Ա̳
Ա̳ Ҿװ Ҿ̳ 83ʱŮ ԱŮװ Ů ׷ Դװ ˼Դ װ Ա װ
首页 > perl > perl里的stat函数

perl里的stat函数

2009年9月9日 发表评论 阅读评论

在perl里,使用stat函数可以得到一个文件的所有属性,下面是perldoc里stat的一个列表
0 dev device number of filesystem
1 ino inode number
2 mode file mode (type and permissions)
3 nlink number of (hard) links to the file
4 uid numeric user ID of file’s owner
5 gid numeric group ID of file’s owner
6 rdev the device identifier (special files only)
7 size total size of file, in bytes
8 atime last access time in seconds since the epoch
9 mtime last modify time in seconds since the epoch
10 ctime inode change time in seconds since the epoch (*)
11 blksize preferred block size for file system I/O
12 blocks actual number of blocks allocated

stat函数的使用很简单,可以像下面这样,打印出所有属性:

1
2
3
4
5
6
7
8
9
#!/usr/bin/perl 
use strict;
use warnings;
 
my $filename = "/tmp/stat.pl";
for(0..12){
    my $d = (stat($filename))[$_];
    print "$d\n";
}

或者也可以使用File::stat模块:

1
2
3
4
use File::stat;
my $b = stat($filename);
printf "File is %s, size is %s, perm %04o, mtime %s\n",
        $filename, $b->size, $b->mode & 07777, scalar localtime $b->mtime;
分类: perl 标签: , 2,319 views

收藏本文:Del.icio.us - Google书签 - 分享到Twitter - 收藏到鲜果 - yahoo收藏 - digg - 更多 »


转载请注明出处及作者信息

本文链接: http://blog.happysa.org/perl/perl_stat.html


Creative Commons License

  1. 2009年9月14日22:25 | #1

    抢沙发~~

  2. wuzhez
    2009年9月14日22:27 | #2

    @lewanzu
    :evil: 抢sf是要上税的

  3. monger
    2010年1月21日00:06 | #3

    更加perl化的写法
    print $_, “\n” foreach stat($filename);

  4. wuzhez
    2010年1月21日10:08 | #4

    @monger
    恩,其他结构化语言落下的毛病

  1. 2011年11月30日15:33 | #1
  2. 2012年1月17日18:11 | #2
注意: 评论者允许使用'@user空格'的方式将自己的评论通知另外评论者。例如, ABC是本文的评论者之一,则使用'@ABC '(不包括单引号)将会自动将您的评论发送给ABC。使用'@all ',将会将评论发送给之前所有其它评论者。请务必注意user必须和评论者名相匹配(大小写一致)。