Opening a File (Revisited)

As you already know, to open a file for reading, you use the following statement:

open FILE_VAR, "<file_in.txt";

And to open a file for writing, you use this statement:

open FILE_VAR, ">file_out.txt";

Perl uses the special character > to indicate that it should open a file for writing and < for a read indicator.

Other special characters are recognized as well. For example, if you want to append to a file, use the >> indicator:

open LOG_FILE, ">>command.log";

Opening a Pipe

Suppose that you are going to write a report to the screen. If you just write the report, the data scrolls off the top of the screen. So to be nice, you want to write out a page of data and wait for the user to press a key. Then you would write out a new page and so on. This behavior is similar to what the more command does. A cheap way of doing this is to require the user to pipe the output of the program to more:

$ perl report.pl | more

Or you could use Perl’s write-to-pipe feature to do the work yourself. For example:

open REPORT_FILE, "| more";

When Perl sees the special character |, it opens a pipe to the command (in this case more) and sends all data written to <REPORT_FILE> to that pipe.

Pipes can be used for input as well. For example, the UNIX/Linux find command finds a list of files and prints the results to screen if needed. Suppose that you want a list of all the files in /release/software that end in .c.

You can locate them with the find command:

find /release/software –name '*.c' –print

Now if you want to get that list into a program, all you have to do is use the open statement with the pipe symbol on the right side of the file specification:

open LIST_FILE, "find /release/software –name '*.c' –print |" or 
    die("Could not execute the find command"); 
my @file_list = <LIST_FILE>; 
chomp(@file_list); 
close (LIST_FILE);

Note that if you are running on Microsoft Windows, you need to use the DIR command in the open statement:

open LIST_FILE, "DIR C:\*.c /s/b |" or 
    die("Could not execute the DIR command");

Open Summary

As you can see in Table 9.1, you can pass many different flags to the open function.

Table 9.1. open Flags
Flag Description
<file.txt Open for reading
>file.txt Open for writing
>>file.txt Append to file
+<file.txt Open file for reading and writing
+>file.txt Create a new file for reading and writing
|command Open a pipe from the Perl program to command (for writing)
command| Open a pipe from the command to the Perl program (for reading)

Using the Backtick (`) Operator

The find example goes to a lot of trouble just to read the output of a command and put it in an array. The backtick (`) operator does this in one operation. For example, you could write find code as

my @file_list = `find /release/software –name '*.c' –print`; 
chomp(@file_list);

The backtick operator (`) executes the command enclosed in the backticks and whatever the command prints. In the array context, it returns an array containing each line of the output. In the scalar context, it returns a string containing the entire output of the program. Lines are separated by newlines.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset