File operations in Perl

Walden Systems Geeks Corner File operations in Perl tutorial how to programming languages Rutherford NJ New Jersey NYC New York North Bergen County
Perl 5 is a highly capable, feature-rich programming language with over 30 years of development. Perl 5 runs on over 100 platforms from portables to mainframes and is suitable for both rapid prototyping and large scale development projects.

File operations are simple, first, we need to associate a filehandle with a file and then we can run operators to read and update the data stream associated with the filehandle. A filehandle is a named internal Perl structure that associates a physical file with a name. All filehandles are capable of read/write access, so we can read from and update any file or device associated with a filehandle. When we associate a filehandle, we can specify the mode in which the filehandle is opened. The three basic file handles are STDIN, STDOUT, and STDERR, which represent standard input, standard output and standard error devices respectively. Perl provides two functions to open files, open() and sysopen().

Open function

Before we can write to or read from a file, we need to open it, asking the operating system to open a channel for your program to access the file. For this Perl provides the open function. We can open a file in read only mode, over write mode, append mode, reads and write mode, or read and write with truncate mode. The syntax for the open function is as follows:

open( SOME_FILE_HANDLE_NAME, "[FILE_MODE_FLAG]FILE_NAME") ;


The following table shows the different file mode flags :

<   r        Read only mode
>   w        Over write mode,  create file if it doesn't exist
>>   a        Append mode,  create file if it doesn't exist
+<  r+        Reads and writes
+<  w+        Reads, writes, creates and truncates files
+<>  a+       Reads, writes, creates and appends files


Sysopen function

The sysopen function is similar to the main open function, except that it uses the system open() function, using the parameters supplied to it as the parameters for the system function. It separates the file operation mode from the file name. The function signature is the following :

sysopen(SOME_FILE_HANDLE_NAME, "FILE_NAME", FILE_MODE_FLAG)


The following table shows the different file mode flags :

O_RDWR         Read and write
O_RDONLY       Read only
O_WRONLY       Write only
O_CREAT        Create a file
O_APPEND       Append a file
O_TRUNC        Truncate a file
O_EXCL         Stop if file already exists

Reading and writing files

Once we have an open filehandle, we are able to read and write information. There are many different ways of reading and writing data into the file. When we use the +>FILEHANDLE+> operator in a list context, it returns a list of lines from the specified filehandle. The read() function reads a block of information from the filehandle's buffer. To write to the file, we use the print() method.

1     open(DATA,";
3     close(DATA);
4
5     open(DATA,">foo.txt") or die "Can't open data";
6     print $DATA "SOME text" ;

Close function

To close a filehandle and disassociate the filehandle from the corresponding file, we use the close function. This flushes the filehandle's buffers and closes the system's file. If no filehandle name is specified, it closes the current, selected file. The close function returns true only if it is successful in flushing the buffer and closing the file.