File Handles and Tests
File Handles and Tests
AND TEST
PRESENTED BY
SANJANA K K
FILE
• HANDLES
A file handle is a internal Perl structure that
associated with a file name .
• Perl file handles are capable of creating , reading,
opening and closing a file.
• File handles are named like other Perl identifiers , but
they since do not have any prefix character , but use
their own namespace, the convention is to used all
uppercase letters for file handle name.
Three Basic File Handle:
1. STDIN
2. STDOUT
3. STDERR
SYNTAX:
open(DATA,”<file.txt”);
Here the < sign indicate that file has to
be opened in read only mode. Here,
DATA is the file handle.
Closing file
SYNTAX:
close(DATA);
Here, DATA is the file handle.
Possible values of different mode
SL.NO ENTITIES AND DEFINITION
1 < or r
Read only access
2 > or w
Creates , write and truncates
3 >> or a
Write , appends and creates
4 +< or r+
Reads and writes
5 +> or w+
Reads ,writes, creates and truncates
6 +>> or a+
Reads ,writes, appends and creates
File test
• Before reading from a file or writing to a file,
it is important to check if the file exists and
readable. In order to perform those tasks, we
• use
Perl Perl
file test operators are
file test operators.logical
operators which
return true or false value.
• The file test operator -e accepts a
filename or file handle as an argument.
Perl file test operators:
• -r: check if the file is readable.
• -w: check if the file is writable.
• -x: check if the file is executable.
• -o: check if the file is owned by effective uid.
• -R: check if file is readable.
• -W: check if file is writable.
• -X: check if file is executable.
• -O: check if the file is owned by real uid.
• -e: check if the file exists.
• -z: check if the file is empty.
• -s: check if the file has nonzero size (returns size
in bytes).
• -f: check if the file is a plain file.
• -d: check if the file is a directory.
• -l: check if the file is a symbolic link.
• -p: check if the file is a named pipe (FIFO): or
Filehandle is a pipe.
• -S: check if the file is a socket.
• -b: check if the file is a block special file.
• -c: check if the file is a character special file.
• -t: check if the file handle is opened to a tty.
• -u-heck if the file has setuid bit set.
• -g: check if the file has setgid bit set.
• -k: check if the file has sticky bit set.
• -T: check if the file is an ASCII text file
(heuristic guess).
• -B: check if the file is a “binary” file
(opposite of -T).
THANK
YOU