Lecture#6
Chapter 11 - Filehandles and File Tests


Filehandles
A filehandle is an I/O connection between your program and the world. They let your program communicate with IO streams and files.

A couple you should already know about:

STDIN - Default input filehandle (keyboard/redirection/pipe)
STDOUT - Default output filehandle (screen/pipe/file)
STDERR - Default error filehandle (screen/file) ./myprogram 2>errlog

In our programs, we'll be concerned with reading and writing to files.

open FH, "filename"         -   opens filename for reading
open FH, "<filename"       -   opens filename for reading
open FH, ">filename"       -   opens filename for writing (overwrite/create)
open FH, ">>filename"     -   opens filename for append

you can also use variables here...

$filename = "myfile";
open FH, "> $filename"; #use the space to avoid crazy appends if $filename starts with >

Filehandles are automatically closed when they are reused or when the program ends. You can also explicitly close them:
close FH;

This flushes the buffer immediately instead of waiting.

When opening a file, you should always make sure the open worked....there's a few ways to do this...

$error = open FH, "<logfile.txt";
if (!$error) {
   print "Unable to open that file!";
}

open FH, "<logfile.txt" or die "Can't open that file!\n"; #Prints msg and ends program

You could get more info on this failure by using one of Perl's special variables:

open FH, "<logfile.txt" or die "Can't open that file: $!\n"; #Prints out some internal info when a system call fails

open FH, "<logfile.txt" or warn "Can't open that file!\n"; #same as before, but execution continues


Using Filehandles
Here's a program that reads in a file and prints all the lines that have the string text in them to an output file.

#!/usr/local/bin/perl
open (IN, "<infile.txt") || die "Can't open the file\n";
open (OUT,">outfile.txt") || die "Can't create output file\n";
while ($input = <IN>) {
   if ($input =~ /txt/) {
      print OUT $input;
   }
}
close OUT;
close IN;


2 More Things To Do With filehandles
Changing the default file handle....

select FH;
print "Hello"; #prints to FH

Flushing the buffer

$|=1; #Now things aren't kept in the buffer


Filetests

if -e $filename {
   print "That file exists...";
}

Filetests are useful if you need to know something about a file before manipulating it. The list of tests are on p. 159 in the book. The 2 I want you to know are:

-e    File/directory exists
-d    File is a directory


Localtime
The localtime function returns a list with a bunch of different items concerning the date and time:

($sec, $min, $hour, $day, $mon, $year, $wday, $yday, $isdst) = localtime;

Gives seconds, minutes, hour, day of month, year-1900, day of week (0-Sun), month (0-Jan), days since Jan 1, is it daylight savings time?.


Bitwise Operators

Similar to other languages....

Operator   MeaningExampleExplanation
&Bitwise AND7 & 2 = 20111 AND 0010 = 0010 = 2
|Bitwise OR7 | 2 = 70111 OR 0010 = 0111 = 7
^Bitwise XOR7 ^ 2=50111 XOR 0010 = 0101 = 5
<<Bitwise left shift7 << 2=280111 shifted 2 bits left= 011100=28
>>Bitwise right shift     7 >> 2=10111 shifted 2 bits right = 0001 = 1
~Bitwise NOTNone good   Depends on how numbers are stored

You can use bitstrings as well, example: "10" | "01" = "11"


Other things in the chapter that we're not going to go over....

Stat and Lstat
Gives info about files...read this section if interested.

Underscore Filehandle
Stores information received from last call of stat or lstat


CSC255 - Alan Watkins - North Carolina State University