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:

     -r  File is readable by effective uid/gid.
-w File is writable by effective uid/gid.
-x File is executable by effective uid/gid.
-o File is owned by effective uid.
-R File is readable by real uid/gid.
-W File is writable by real uid/gid.
-X File is executable by real uid/gid.
-O File is owned by real uid.
-e File exists.
-z File has zero size (is empty).
-s File has nonzero size (returns size in bytes).
-f File is a plain file.
-d File is a directory.
-l File is a symbolic link.
-p File is a named pipe (FIFO), or Filehandle is a pipe.
-S File is a socket.
-b File is a block special file.
-c File is a character special file.
-t Filehandle is opened to a tty.
-u File has setuid bit set.
-g File has setgid bit set.
-k File has sticky bit set.
-T File is an ASCII text file (heuristic guess).
-B File is a "binary" file (opposite of -T).
-M Script start time minus file modification time, in days.
-A Same for access time.
-C Same for inode change time (Unix, may differ for other
platforms)

you can get -X from http://perldoc.perl.org/functions/-X.html

Perl file checking --- How to get information about a file的更多相关文章

  1. backup, file manipulation operations (such as ALTER DATABASE ADD FILE) and encryption changes on a database must be serialized.

    昨天在检查YourSQLDba备份时,发现有台数据库做备份时出现了下面错误信息,如下所示: <Exec>   <ctx>yMaint.ShrinkLog</ctx> ...

  2. ASM ClassReader failed to parse class file - probably due to a new Java class file version that isn't supported yet

    严重: Context initialization failedorg.springframework.beans.factory.BeanDefinitionStoreException: Fai ...

  3. The URL "filename" is invalid. It may refer to a nonexistent file or folder, or refer to a valid file or folder that is not in the current Web

    Sharepoint Error : The URL "filename" is invalid. It may refer to a nonexistent file or fo ...

  4. ASM ClassReader failed to parse class file - probably due to a new Java class file version that isn't supported yet………

    在照着这里例子学习ssm时,在部署阶段遇到了这个问题“ASM ClassReader failed to parse class file - probably due to a new Java c ...

  5. [转]ADT中通过DDMS导入文件出错ddms transfer error: Read-only file system,Failed to push selection: Read-only file system

    [已解决] 原文  http://www.crifan.com/ddms_import_file_error_transfer_error_read_only_file_system/ 想要通过adt ...

  6. ORA-01146: cannot start online backup - file 1 is already in backup ORA-01110: data file 1: 'C:\ORACLE\ORADATA\ORCL8\SYSTEM01.DBF'

    问题: Error: [1146] ORA-01146: cannot start online backup - file 1 is already in backup ORA-01110: dat ...

  7. Warning: File upload error - unable to create a temporary file in Unknown on line 0

    upload_tmp_dir 临时文件夹问题 上传文件提示 Warning: File upload error - unable to create a temporary file in Unkn ...

  8. File upload error - unable to create a temporary file

    php上传图片的时候会报错: File upload error - unable to create a temporary file 文件上传错误 - 无法创建一个临时文件 你只需要打开你的php ...

  9. PHP Warning: File upload error - unable to create a temporary file in Unknown on line 0

    代码在本地运行一切都OK,放到服务器上,网站访问正常,上传就出现该错误. 提示:PHP Warning: File upload error - unable to create a temporar ...

随机推荐

  1. Teamwork(The fourth day of the team)

    在这天我们已经开始去做自己手上的的任务.由于我们都忙于手头上的工作,所以这天我们就没有过多的交流,有的可能就是网上说一下实现到了哪里.

  2. 结对项目作业报告——四则运算web项目

    成员:顾思宇2016011993 程羚2016012050   1.仓库地址:https://git.coding.net/DandelionClaw/WEB_Calculator.git 注: 本项 ...

  3. ADT图及图的实现及图的应用

    图: 图中涉及的定义: 有向图: 顶点之间的相关连接具有方向性: 无向图: 顶点之间相关连接没有方向性: 完全图: 若G是无向图,则顶点数n和边数e满足:0<=e<=n(n-1)/2,当e ...

  4. 消息队列第一篇:MessageQueue介绍

    消息队列有哪些好处或功能: 1.消息可以在断开连接的环境下发送.不需要同时运行正在发送和正在接收的应用程序. 2.使用快捷模式,消息可以非常快地发送.在快捷模式下,消息存储在内存中. 3.对于可恢复的 ...

  5. redis简介及增删改查

    redis 是一个文档(nosql)数据库,工作与内存,主要用做高速缓存 缓存经常会查到的数据 存入的值默认是字符串 使用步骤: 1 从redis.io下载 2 点击redis-server.exe启 ...

  6. Beta阶段团队项目开发篇章4

    例会时间 2016.12.11 例会照片 个人工作 上阶段任务验收 删除robot功能较为简单,基本实现,但是一直Question版块任务量过大,考虑到最近要进行编译课设第二次测试,为该任务再分配一段 ...

  7. Selenium WebDriver 中鼠标和键盘事件分析及扩展(转)

    文章转自:http://www.ibm.com/developerworks/cn/java/j-lo-keyboard/

  8. 程序集里包含多个版本dll引用 ,强制低版本到制定版本dll引用

    在 config 的 <configuration> 节点内加入以下 类似信息 以下是以Newtonsoft.Json 为例子 <runtime> <assemblyBi ...

  9. MySQL复制 -- binlog(2)

    MySQL复制是使用最为广泛的一套组建,上一节已经简单说了一下复制的一些用途和复制的原理,知道了这些我们能够快速的搭建起复制的平台,但是仅知道这些还是不够的,很多时候并不是一帆风顺的,总会有那么一小段 ...

  10. 题解 P3870 【[TJOI2009]开关】

    这个题我愣是交了好几遍没有过...... 后来@_皎月半洒花dalao告诉我说要^儿子节点的tag,然后就明白了...... 行吧,先上题面: 题目描述 现有N(2 ≤ N ≤ 100000)盏灯排成 ...