Working with Files in Perl

Now let’s look at how files are handled from within Perl programs. Redirection operators are used when files are opened in Perl to indicate how the file will be used. Because CGI programs so often work with external files, it’s important to understand how Perl handles these operations.

To access an external file from within a Perl function, the open function is used. It accepts two arguments, the filehandle that will be used to refer to the file, and the specification for the file to be opened. Here’s an example:

open (FILE, "< this_file.html"); 

The first argument is FILE. When I want to retrieve data from the file later in the program, I’ll use the filehandle FILE to do so. You don’t have to make your filehandles ...

Get Sams Teach Yourself CGI in 24 Hours, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.