文件测试操作符
-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
-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
-e File or directory name exists
-z File exists and has zero size (always false for directories)
-s File or directory exists and has nonzero size (the value is the size in bytes)
-f Entry is a plain file
-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 filehandle is a TTY (as reported by the isatty() system function; filenames can’t be tested by this test)
-T File looks like a “text” file
-B File looks like a “binary” file
-M Modification age (measured in days)
-A Access age (measured in days)
-C Inode-modification age (measured in days)
测试操作符后面跟的是文件名或文件句柄,如果不跟,默认对象为$_。
示例如下:
die "Oops! A file called '$filename' already exists.\n"
if -e $filename; #如果存在文件$filename就停止程序
对同一文件的多项属性进行测试时,可以用虚拟文件句柄“_”来避免多次读取文件属性:
if (-r $file and -w
_){
...
}
if (-x
_) {
...
}
perl5.10中导入的栈式文件测试操作可以一次完成一个文件的多个测试:
use 5.010;
if ( -r -w $file) {
...
}
stat和istat函数
函数stat的参数可以是文件句柄(包括虚拟文件句柄_),或是某个会返回文件名的表达式。如果执行失败,返回空列表;要不然就返回一个含13个数字元素的列表。具体如下:
my($dev, $ino, $mode, $nlink, $uid, $gid, $rdev,
$size, $atime, $mtime, $ctime, $blksize, $blocks)
= stat($filename);
这里的变量对应stat函数返回的列表数据。
$dev和$ino 文件所在设备编号与文件inode编号。它们两个决定了这个文件的唯一性。
$mode 文件的权限位集合,还包括其他信息位。
$nlink 文件或目录的硬连接数
$uid 与$gid 文件拥有者的userid和groupid
$size 以字节为单位的文件大小
$atime,$mtime和$ctime 32位整数的时间戳
对符号链接名调用stat函数,返回的是符号链接指向对象的信息。如果要获得符号链接本身信息,需要用函数lstat,用法同stat。
localtime函数可以把时间戳转换成容易阅读的形式,示例如下:
my $timestamp = 1180630098;
my $date = localtime $timestamp;
转换成的格式如下:
my($sec, $min, $hour, $day, $mon, $year, $wday, $yday, $isdst)
= localtime $timestamp;
$mon是0-11的月份值
$year是1900年起的年数
$wday是0-6(周日到周六)的星期
$yday是0-364或365(1月1日到12月31日的天数)
gmtime函数跟localtime函数一样,只是返回的是世界标准时间(格林威治)。如果需要从系统时钟获得当前时间戳,可以用time函数。不提供参数情况下,localtime和gmtime默认使用当前time函数返回的时间值。
按位运算操作符(bitwise operators)
& 按位与(bitwise and) 比较左右两边的对应位,若都为真则为真,反之为假
| 按位或(bitwise or) 比较左右两边的对应位,若任一为真则为真,都为假则假
^ 按位异或(bitwise xor) 比较左右两边的对应位,若一边真一边假则为真,反之为假
<< 按位左移(bitwise shift left) 将左边操作数向左移动位数,移动位数由右边操作数决定,以0填补最低位
>> 按位右移(bitwise shift right) 跟以上相反,并且丢弃移除的最低位
~ 按位取反(bitwise negetion) 把后面的操作数每一位取反
位字符串操作符(bitwise string operators)
按位运算操作符可以操作整数也可以操作字符串。只有当所有的操作数都是字符串时,位运算操作符才会作为位字符串操作符来工作。可以用“”或0+来显式指定操作数为字符串或数字:
- $foo = 150 | 105; # yields 255 (0x96 | 0x69 is 0xFF)
- $foo = '150' | 105; # yields 255
- $foo = 150 | '105'; # yields 255
- $foo = '150' | '105'; # yields string '155' (under ASCII)
- $baz = 0+$foo & 0+$bar; # both ops explicitly numeric
- $biz = "$foo" ^ "$bar"; # both ops explicitly stringy
进行位字符串操作时,两边的字符串从左端开始对齐,按照具体的操作符截断长字符串的右端或对段字符串右端进行补零。示例如下:
- # ASCII-based examples
- print "j p \n" ^ " a h"; # prints "JAPH\n"
- print "JA" | " ph\n"; # prints "japh\n"
- print "japh\nJunk" & '_____'; # prints "JAPH\n";
- print 'p N$' ^ " E; # prints "Perl\n";
参考: