Cari di Perl 
    Perl User Manual
Daftar Isi
(Sebelumnya) Test or set particular bits in ...Change your current working di ... (Berikutnya)
Functions for filehandles, files, or directories

A file test (-r, -x, etc)

Daftar Isi

  • -X FILEHANDLE

  • -X EXPR
  • -X DIRHANDLE
  • -X

    A file test, where X is one of the letters listed below. This unaryoperator 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 theargument is omitted, tests $_, except for -t, which tests STDIN.Unless otherwise documented, it returns 1 for true and '' for false, orthe undefined value if the file doesn't exist. Despite the funnynames, precedence is the same as any other named unary operator. Theoperator 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 platforms)

    Example:

    1. while (<>) {
    2. chomp;
    3. next unless -f $_; # ignore specials
    4. #...
    5. }

    Note that -s/a/b/ does not do a negated substitution. Saying-exp($foo) still works as expected, however: only single lettersfollowing a minus are interpreted as file tests.

    These operators are exempt from the "looks like a function rule" describedabove. That is, an opening parenthesis after the operator does not affecthow much of the following code constitutes the argument. Put the openingparentheses before the operator to separate it from code that follows (thisapplies only to operators with higher precedence than unary operators, ofcourse):

    1. -s($file) + 1024 # probably wrong; same as -s($file + 1024)
    2. (-s $file) + 1024 # correct

    The interpretation of the file permission operators -r, -R,-w, -W, -x, and -X is by default based solely on the modeof the file and the uids and gids of the user. There may be otherreasons you can't actually read, write, or execute the file: forexample network filesystem access controls, ACLs (access control lists),read-only filesystems, and unrecognized executable formats. Notethat the use of these six specific operators to verify if some operationis possible is usually a mistake, because it may be open to raceconditions.

    Also note that, for the superuser on the local filesystems, the -r,-R, -w, and -W tests always return 1, and -x and -X return 1if any execute bit is set in the mode. Scripts run by the superusermay thus need to do a stat() to determine the actual mode of the file,or temporarily set their effective uid to something else.

    If you are using ACLs, there is a pragma called filetest that mayproduce more accurate results than the bare stat() mode bits.When under use filetest 'access' the above-mentioned fileteststest whether the permission can(not) be granted using theaccess(2) family of system calls. Also note that the -x and -X mayunder this pragma return true even if there are no execute permissionbits set (nor any extra execute permission ACLs). This strangeness isdue to the underlying system calls' definitions. Note also that, due tothe implementation of use filetest 'access', the _ specialfilehandle won't cache the results of the file tests when this pragma isin effect. Read the documentation for the filetest pragma for moreinformation.

    The -T and -B switches work as follows. The first block or so of thefile is examined for odd characters such as strange control codes orcharacters with the high bit set. If too many strange characters (>30%)are found, it's a -B file; otherwise it's a -T file. Also, any filecontaining a zero byte in the first block is considered a binary file. If -Tor -B is used on a filehandle, the current IO buffer is examinedrather than the first block. Both -T and -B return true on an emptyfile, or a file at EOF when testing a filehandle. Because you have toread a file to do the -T test, on most occasions you want to use a -fagainst the file first, as in next unless -f $file && -T $file.

    If any of the file tests (or either the stat or lstat operator) is giventhe special filehandle consisting of a solitary underline, then the statstructure of the previous file test (or stat operator) is used, savinga system call. (This doesn't work with -t, and you need to rememberthat lstat() and -l leave values in the stat structure for thesymbolic link, not the real file.) (Also, if the stat buffer was filled byan lstat call, -T and -B will reset it with the results of stat _).Example:

    1. print "Can do.\n" if -r $a || -w _ || -x _;
    2. stat($filename);
    3. print "Readable\n" if -r _;
    4. print "Writable\n" if -w _;
    5. print "Executable\n" if -x _;
    6. print "Setuid\n" if -u _;
    7. print "Setgid\n" if -g _;
    8. print "Sticky\n" if -k _;
    9. print "Text\n" if -T _;
    10. print "Binary\n" if -B _;

    As of Perl 5.9.1, as a form of purely syntactic sugar, you can stack filetest operators, in a way that -f -w -x $file is equivalent to-x $file && -w _ && -f _. (This is only fancy fancy: if you usethe return value of -f $file as an argument to another filetestoperator, no special magic will happen.)

    Portability issues: -X in perlport.

    To avoid confusing would-be users of your code with mysterioussyntax errors, put something like this at the top of your script:

    1. use 5.010; # so filetest ops can stack
 
Source : perldoc.perl.org - Official documentation for the Perl programming language
Site maintained by Jon Allen (JJ)     See the project page for more details
Documentation maintained by the Perl 5 Porters
(Sebelumnya) Test or set particular bits in ...Change your current working di ... (Berikutnya)