Home » Perl Basics » 09 - Input / Output
9

Using Files

What is File I/O and what role does it play in the Perl language?

File I/O is a big part of Perl. Depending on the function you will use you will either have to une an integrated function or an external one (more on that later).
The first function is the function open. As its name indicated open is the function to open a file.

  open (FILEHANDLE, “data-file”) || die “error $!”;
This is the standard open function syntax. The || means “or” it allow you to run another function if the open fails. In that case “die”. “die” will stop the script and return the error message stored in $!. “$!” is a variable intrernal to perl that stores the last error code produced during the script.
The open function can be use to open but also create or append files.
  open (FILEHANDLE, “>data-file”) || die “error $!”;
The syntax creates a file and open it to write.
  open (FILEHANDLE, “>>data-file”) || die “error $!”;
That syntax open a file in appends mode

First lets create a file and fill it.

  #!/usr/bin/perl 
  #author : you
  #date : 5.10.2007
  #purpose : File I/O
  open (FILEHANDLE, “>File1”) || die “error $!”;
  print (FILEHANDLE, “success”);
  #END
  

This simple script will create a file called file1 and write “success” in it.
FILEHANDLE represent the file in memory. Instead of using the actual name of the file the open function take as an argument whatever you want to call the file. It has to be capitalized. It if commonly recommended to stick with a FILEHANDLE name of INFILE and OUTFILE. The second argument is the name of the file. If no path it implied the file is open or written in the folder the script is.
Now lets see how to open and manipulate data in a file.
Opening and displaying the content of a file :

  open (INFILE, “file1”) || die “error $!”;
  while (<INFILE>) {
  print “$_”;
  }
  
This script will return each line of the script ON ITS OWN LINE. No need to put a “\n” new line character after each loop.

 
  open (INFILE, “file1”) || die “error $!”;
  @array1 = <INFILE>; 
  
Most of the data manipulation will require the file to be put in a array. This scrip do that. Each line will be put into one element of the array.
You can, and in fact are encourage to use cariable for the file name and file handle.
Copying a file:
We say how to create a file and how to read one. Copying a file is only matter to combine those 2 functin.
  open (INFILE, “filetoread”) || die “error $!”;
  open (OUTFILE, “>filetowrite” || die “ error : $!”;
  @filetoread = <INFILE>;
  for each (@filetoread) {
  print OUTFILE $_;
  }
  
Note: That kind of script will not return anything to the display if it is executed without errors
Appending a file requires only a small modification of the write script
  #!/usr/bin/perl 
  #author : you
  #date : 5.10.2007
  #purpose : File I/O append
  open (FILEHANDLE, “>>File1”) || die “error $!”;
  print (FILEHANDLE, “success number 2”);
  #END
  
This script will take the file1 we created earlier and write “success number 2” at the end of the file without touching to anything previously written in that file.