You’ve seen that Perl provides three filehandles—
STDERR—which are automatically open to files or devices established by the program’s parent process (probably the shell). When you need other filehandles, use the
open operator to tell Perl to ask the operating system to open the connection between your program and the outside world. Here are some examples:
open CONFIG, "dino"; open CONFIG, "<dino"; open BEDROCK, ">fred"; open LOG, ">>logfile";
The first one opens a filehandle called
CONFIG to a file called
dino. That is, the (existing) file
dino will be opened and whatever it holds will come into our program through the filehandle named
CONFIG. This is similar to the way that data from a file could come in through
STDIN if the command line had a shell redirection like
<dino. The second example uses the same sequence; it does the same as the first, but the less-than sign explicitly says “use this filename for input,” even though that’s the default.
Though you don’t have to use the less-than sign to open a file for input, we include that because, as you can see in the third example, a greater-than sign means to create a new file for output. This opens the filehandle
BEDROCK for output to the new file
fred. Just as when the greater-than sign is used in shell redirection, we’re sending the output to a new file called
fred. If a file has that name, we’ll wipe it out and replace it with this new one.
The fourth example shows how two greater-than ...