设为首页 收藏本站
查看: 939|回复: 0

[经验分享] Perl file checking

[复制链接]

尚未签到

发表于 2015-12-26 16:27:37 | 显示全部楼层 |阅读模式
  There are some short expressions in Perl that allow you to test files, which is handy if you want to check what a file allows before using it, or if it exists at all. We'll look at a few of the common tests in this section.

Existence
  To see if a file exists before using it, we can use:



if (-e "filename.cgi")
{   
  #proceed with your code

}
  
  The -e part is the existence test. After that we have a space, followed by the file we want to test. As in the other sections, we could also use a variable to hold the file name:



$neededfile="myfile.cgi";
if (-e $neededfile)
{   
  #proceed with your code

}
  
  Now, you can do something based on whether or not the file is there. If it is not, you can give an error page or move on to something else.
  Two other existence tests you can use may help if you need to know whether or not the file has anything in it:
File exists, has a size of zero: -z
File exists, has non-zero size: -s

Readable, Writable, or Executable
  To see if the file is allowed to be read, written to, or executed we can use these:
Readable: -r
Writable: -w
Executable: -x
  So, if we want to check whether we can read a file before we try to open it, we could do this:



$readfile="myfile.cgi";
if (-r $readfile)
{   
  #proceed with your code

}
  
  The same goes for the writable and executable tests. These can be a handy way to keep from trying to write to files that don't have write permissions, and various other things.

Text or Binary
  You can test the file to see if it is text or if it is binary using these:
Text File: -T
Binary File: -B
  They work the same way as the others as well.

Multiple Tests
  You can test for two or more things at a time using the "and" (&&) or the "or" ( || ) operators. So, if you want to know if a file exists and is readable before opening it, you could do this:



$readfile="myfile.cgi";
if ( (-e $readfile) && (-r $readfile) )
{   
  #proceed with your code

}
  
  File tests are helpful in applications that make use of files often in the code. Using these can help avoid errors, or alert you to an error that needs to be fixed (a required file not able to be read, for instance). Well, have fun testing those files!
  
  -----------Comment by Orientsun--------------
  In addion, Perl provide a X parameter (Perl said -X is a special function) to us to checking file's information, the details of X as bellow:


  • -X FILEHANDLE


  • -X EXPR
  • -X DIRHANDLE
  • -X
  A file test, where X is one of the letters listed below. This unary operator takes one argument, either a filename, a filehandle, or a dirhandle, and tests the associated file to see if something is true about it. If the argument is omitted, tests $_ , except for -t , which tests STDIN. Unless otherwise documented, it returns 1for true and '' for false, or the undefined value if the file doesn't exist. Despite the funny names, precedence is the same as any other named unary operator. The operator may be any of:



1     -r  File is readable by effective uid/gid.
2     -w  File is writable by effective uid/gid.
3     -x  File is executable by effective uid/gid.
4     -o  File is owned by effective uid.
5     -R  File is readable by real uid/gid.
6     -W  File is writable by real uid/gid.
7     -X  File is executable by real uid/gid.
8     -O  File is owned by real uid.
9     -e  File exists.
10     -z  File has zero size (is empty).
11     -s  File has nonzero size (returns size in bytes).
12     -f  File is a plain file.
13     -d  File is a directory.
14     -l  File is a symbolic link.
15     -p  File is a named pipe (FIFO), or Filehandle is a pipe.
16     -S  File is a socket.
17     -b  File is a block special file.
18     -c  File is a character special file.
19     -t  Filehandle is opened to a tty.
20     -u  File has setuid bit set.
21     -g  File has setgid bit set.
22     -k  File has sticky bit set.
23     -T  File is an ASCII text file (heuristic guess).
24     -B  File is a "binary" file (opposite of -T).
25     -M  Script start time minus file modification time, in days.
26     -A  Same for access time.
27     -C  Same for inode change time (Unix, may differ for other
28     platforms)
  you can get -X from http://perldoc.perl.org/functions/-X.html

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-156701-1-1.html 上篇帖子: ASP.NET/Perl.NET (转) 下篇帖子: awk、perl对多个文件取交集
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表