blue1025的个人空间 https://blog.eetop.cn/mj8051 [收藏] [复制] [分享] [RSS]

空间首页 动态 记录 日志 相册 主题 分享 留言板 个人资料

日志

PERL 第11章 文件测试

已有 804 次阅读| 2008-9-27 14:41 |个人分类:PERL

SOURCE HTML: http://socvista.com/bbs/redirect.php?fid=71&tid=930&goto=nextnewset

第11章 文件测试

== 文件测试operators

列表说明,简单易懂。
--  test -------------- Meaning ------------------------------
-r         File or directory is readable by this effective user or group
-w        File or directory is writable by this effective user or group
-x         File or directory is executable by this effective user or group
-o        File or directory is owned by this effective user or group
-R        File or directory is readable by this real user or group
-W       File or directory is writable by this real user or group
-X        File or directory is executable by this real user or group
-O       File or directory is owned by this real user or group
-e        File or directory name exists
-z         File exists and has zero size (always false for directory)
-s         File or directory exists and has nonzero size (the value is the size in bytes)
-f          Entry is a plain file           ## unix系统中,任意item必为下面7种类型之一
-d         Entry is a directory
-l          Entry is a symbolic link
-S        Entry is a socket
-p        Entry is a named pipe (a fifo)
-b        Entry is a block-special file (like a mountable disk)
-c        Entry is a character-special file (like an I/O device)
-u        File or directory is setuid
-g        File or directory is setgid
-k        File or directory has the sticky bit set
-t         the file handle is a TTY     ## 用 -t STDIN 判断是否为 true (表示来自TTY,可以交互),否则来自file/pipe,不可交互
-T        File looks like a text file
-B        File looks like a binary file
-M       Modification age (in days)
-A       Access age (in days)
-C       Inode-modification age (in days)

== 文件的统计信息 stat 和 lstat

使用方法
my ( $dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat( $filename );
stat函数返回13个信息。
$dev:  device number
$ino:  inode number
$mode:  就是用ls -l 可以看到的权限信息等,比如0755
$nlink:  link到本文件的数目
$uid:  user ID
$gid:  group ID
$size:  in bytes
$atime, $mtime, $ctime:  32位的数字,记录事件发生时的距离EPOCH的秒数。EPOCH在有些系统中是1970年的开始。
对于symbolic link,要用lstat来获得这些信息。

== 时间信息
有两种时间,看例子
1. my $now = localtime;  # 本地时间
2. my $now = gmtime;  # Greenwich Mean Time
这两种时间的格式都是一个复杂的数字,无法看懂。其实他是一个list构成的。我们来看。

my($sec, $min, $hour, $day, $mon, $year, $wday, $yday, $isdst) = localtime;

也就是说,localtime其实是一个9项的list。大部分项无需说明,一看便知,说几个特别的。
$mon是从0计数到11的,注意。
$year是计算的从1900开始的偏移年数。
$wday是表示今天星期几。
$yday表示今天是一年中的第几天。
$isdst表示是否使用了夏令时。

== 位运算符号

& 表示位与运算
|  表示位或运算
^  表示位异或运算
<<  表示左移若干比特
>> 表示右移若干比特
~  表示所有比特取反

== 特殊下划线 file handle

看一个例子。
foreach (@original_files) {
  push @big_old_files, $_
    if (-s) > 100_000 and -A   _ > 90;
}
注意最后那个if 语句。有一个下划线。那个下划线表示file test可以根据上次的buffer内容来进行,而无需再次访问真实的操作系统来获得。因为每次通过file handle访问文件属性,都要和OS打交道,并保留一个buffer,为了节省时间,就想出了这种办法:不经过OS,而是直接访问上次的buffer。
注意:这种方法有可能出现严重错误。比如由于程序的复杂性,上次的buffer并不是这次期待的那个。因此,复杂程序不推荐使用这个特性。



点赞

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 注册

  • 关注TA
  • 加好友
  • 联系TA
  • 0

    周排名
  • 0

    月排名
  • 0

    总排名
  • 0

    关注
  • 13

    粉丝
  • 16

    好友
  • 15

    获赞
  • 23

    评论
  • 2824

    访问数
关闭

站长推荐 上一条 /1 下一条

小黑屋| 关于我们| 联系我们| 在线咨询| 隐私声明| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 )

GMT+8, 2024-4-28 08:22 , Processed in 0.013366 second(s), 7 queries , Gzip On, Redis On.

eetop公众号 创芯大讲堂 创芯人才网
返回顶部