Perl File/Dir Operations

File Test Operators

-r -w -x  File is readable/writable/executable by effective uid/gid.

next unless -f $file;
if(-r “test.pl”) { print “readable\n” }

-R -W -X File is readable/writable/executable by real uid/gid.
-o -O File is owned by effective/real uid.
-e -z File exists/has zero size.
-s File exists and has non-zero size. Returns the size.
-f -d File is a plain file/a directory.
-l -S -p File is a symbolic link/a socket/a named pipe (FIFO).
-b -c File is a block/character special file.
-u -g -k File has setuid/setgid/sticky bit set.
-t Tests if filehandle (STDIN by default) is opened to a tty.
-T -B File is a text/non-text (binary) file.
-T and -B return true on a null file, or a file at EOF when testing a filehandle.
-M -A -C File modification / access / inode-change time.
Measured in days. Value returned reflects the file age at the time the script started.
search on $^T in the Special Variables
next unless -M $file > .5; # older then 12 hours



File Operations

chmod LIST Changes the permissions of a list of files.
The first element of the list must be the numerical mode.
chown LIST Changes the owner and group of a list of files.
The first two elements of the list must be the numerical uid and gid.
truncate FILE, SIZE Truncates FILE to SIZE. FILE may be a filename or a filehandle.
link OLDFILE, NEWFILE Creates a new filename linked to the old filename.
lstat FILE Like stat, but does not traverse a final symbolic link.
mkdir DIR, MODE Creates a directory with given permissions. Sets $! on failure.
readlink EXPR Returns the value of a symbolic link.
rename OLDNAME, NEWNAME Changes the name of a file.
rmdir NAME Deletes the directory if it is empty. Sets $! on failure.
stat FILE Returns a 13-element array
(0: $dev, 1: $ino, 2: $mode, 3: $nlink, 4: $uid, 5: $gid,
6: $rdev, 7: $size, 8: $atime, 9: $mtime, 10: $ctime,
11: $blksize, 12: $blocks).
FILE can be a filehandle, an expression evaluating to a filename,
or _ to refer to the last file test operation or stat call. Returns a null list if the stat fails.
symlink OLDFILE, NEWFILE Creates a new filename symbolically linked to the old filename.
unlink LIST Deletes a list of files.
utime LIST Changes the access and modification times.
The first two elements of the list must be the numerical access and modification times.




The following filename conventions apply when opening a file.

“FILE”    open FILE for input. also “<FILE”.
“>FILE”    open FILE for output, creating it if necessary.
open(LOG,”>$access_log”) || die “Can’t open User Access Log: $!\n”;
“>>FILE”    open FILE in append mode.   open(LOG,”>>$access_log”);
“+<FILE”    open FILE with read/write access (file must exist).
“+>FILE”    open FILE with read/write access (file truncated).
“|CMD”    opens a pipe to command CMD; forks if CMD is. open(LOG,”| ausgabe”);
“CMD|”    opens a pipe from command CMD; forks if CMD is.
open(LOG,”eingabe |”);
open(DATUM,”date |”);
open(WORT,”| wc > wort.dat”); # wc : counter

Post your comment

*

code

 

 

See more website here...

  • Odlinks
  • Farsi-News
  • Advertise Here