Perl Example #3
Working with Files in Perl

About the Program

The following program demonstrates some of the power of Perl when working with files. It shows two techniques for reading in files. The first method is called "slurping" where the entire file is read into memory, and the second is a more standard approach where one line is read in at a time. This example also shows how various file attributes can be accessed from the Perl code.

In addition to file operations, the program also uses some additional pattern matching and substitution operations. With the second file, the program will use the translate function "tr" to translate input lines to all uppercase letters as well as the substitution operator "s" to relace characters and strings. Many of these operations work with "$_", the default scalar variable in Perl. In most perl programs, this variable is not written out in the code, but is just understood to be the operand.

#!/usr/bin/perl -w
#Experimenting with files

print "Enter filename:";
chomp($fname = <STDIN>);

## Open a file.  If unsuccessful, print an error message and quit.

open (FPTR,$fname) || die "Can't Open File: $fname\n";

# First Technique: "Slurping"
# This approach reads the ENTIRE file into memory 
# Caution... This is not a good method for BIG files!!!

@filestuff = <FPTR>;  #Read the file into an array
print "The number of lines in this file is ",$#filestuff + 1,"\n";
print @filestuff;
Enter filename:  temp.txt
The number of lines in this file is 4

This is my file,
It's all full of text.
Upcase the chars,
And screw up what's left.
close (FPTR);         #Close the file

## Some other useful capabilities
## Testing file attributes:

print "Enter another filename:";
chomp($fname = <STDIN>);

if (-T $fname)   # Check if it's a textfile, and how old
   {
    print "File $fname is textfile. ";
    print "It was modified ", int(-M $fname), " days ago.\n";
    open (FPTR,$fname) || die "Sorry.  Can't Open File: $fname\n";
   }

elsif (-B $fname)   # Check if it's a binary file, and some other stuff
   {
    print "File $fname is executable.\n" if (-x $fname);
    print "This file is ", -s $fname, " bytes.\n";
    die "Since it is Binary file, we will not try to \"upcase\" this file.\n";
   }
else
   {die "File $fname is neither text nor binary, so it may not exist. \n" ;
   }
Enter another filename: window1
File window1 is executable.
This file is 27141 bytes.
Since it is Binary file, we will not try to "upcase" this file.
    Enter another filename: temp.txt
File temp.txt is textfile. It was modified 2 days ago.


## Open a file for writing.  Note UNIX-like I/O redirection symbol, ">".

open (OUTFILE, ">upcase.txt") || die "Can't oupen output file.\n";

    
## Better approach for large files... Work with just current input line.

while (<FPTR>)  # While still input lines in the file...
  {
   print "1. ",$_;     # The symbol "$_" is the default variable, the current
                 #   input from file.  Note: "$_" is assumed if left out.

   tr/a-z/A-Z/;  # Translate all lower case letters to uppercase letters
                 #   in the default variable.
   
   print "2. ", $_;
   s/A/@/g;      # More substitutions:  All "A" chars become "@" signs.
   print"3. ", $_;
   s/UP/Down/g;  # All "UP" words are replaced by the string "Down" 
   print "4. ", $_;
   $pattern = '\sF(.*)L';  # Meaning of Regular Expression:
                           # \sF - starts with a  and capital F
                           # .*  - some stuff in between
                           # L  - Has a capital L in it

			   # The parentheses "mark" the stuff in between

   print "   Match value: ", $1, "\n" if (/$pattern/);; 

   s/$1/*/g if $_ =~ $pattern; # Substitute "*" for the marked pattern,
                               #  but anywhere within the line.
   print "5. ", $_, "\n";
   print OUTFILE $_;  # Print default variable to OUTFILE.
  }

close (FPTR);   # Close the other two files
close (OUTFILE);
1. This is my file,
2. THIS IS MY FILE,
3. THIS IS MY FILE,
4. THIS IS MY FILE,
Match value: I
5. TH*S *S MY F*LE,

1. It's all full of text.
2. IT'S ALL FULL OF TEXT.
3. IT'S @LL FULL OF TEXT.
4. IT'S @LL FULL OF TEXT.
Match value: UL
5. IT'S @LL F*L OF TEXT.

1. Upcase the chars,
2. UPCASE THE CHARS,
3. UPC@SE THE CH@RS,
4. DownC@SE THE CH@RS,
5. DownC@SE THE CH@RS,

1. And screw up what's left.
2. AND SCREW UP WHAT'S LEFT.
3. @ND SCREW UP WH@T'S LEFT.
4. @ND SCREW Down WH@T'S LEFT.
5. @ND SCREW Down WH@T'S LEFT.

The actual program: ex3.pl

The sample text file: temp.txt

The output: ex3.out

dhyatt@thor.tjhsst.edu