Using Perl and Regular Expressions to Process HTML Files - Part 4

by John Dixon - Date: 2006-12-04 - Word Count: 421 Share This!

In Part 1 we had a quick look at what Perl and regular expressions are, and introduced the idea of using them to process HTML files. In Part 2 we developed a Perl script to process a single HTML file. In part 3 we looked at one way of processing multiple files. In this part we'll look at an additional way to import files for processing.

In Part 3 we wrote a script ( that enabled us to enter filenames at the command prompt:

c:>perl file1.htm file2.htm file3.htm

Although this script enables us to process as many files as we want to, the drawback is that all the filenames need to be manually typed in. This is fine if you only want to process a few files, but if you've got hundreds or thousands to process, this approach would not be feasible.

Note: Due to display considerations, in the example code shown in this article, square brackets '[..]' are used in HTML/script tags instead of angle brackets ''.

1 foreach $file (@ARGV) {
2 rename $file, "$file.bak";
3 open (IN, "$file");
5 while ($line = [IN]) {
6 $line =~ s/[h1]/[h1 class="big"]/;
7 (print OUT $line);
8 }
9 close IN;
10 close OUT;
11 }

In, it's line 1 that enables us to enter filenames at the command prompt., which is listed below, provides us with a way to process all the HTML files (that have a .htm extension) in the current directory/folder. This is the directory where all the files to be processed, and the script itself, are located.

1 opendir(DIR, ".") or die "can't opendir: $!";
2 @allfiles = grep (/.htm$/i, readdir DIR);
3 closedir(DIR);
4 foreach $name (@allfiles) {
5 rename $file, "$file.bak";
6 open (IN, "$file");
8 while ($line = [IN]) {
9 $line =~ s/[h1]/[h1 class="big"]/;
10 (print OUT $line);
11 }
12 close IN;
13 close OUT;
14 }

The only difference between and is the first few lines. Let's look at the new lines in

Line 1
Opens the current directory (signified by a dot ".") for processing. It is given a directory handle of DIR. If the directory cannot be opened, an error message is displayed.

Line 2
This line reads in all the ,htm files in the directory, and puts them in an array called @allfiles. In Perl, a '@' indicates an array, and a '$' indicates a variable. A variable stores a single value, whereas an array stores a list of values.

grep is a search command from the UNIX world.

Note that there should be a backslash character directly before the '.htm', but it isn't being displayed.

Line 3
This line closes the DIR directory handle.

Running the script


In Part 5 we'll look at how to read in specific files from specific directories.

Related Tags: html, perl, regular, expression, expressions, convert, conversion, process, processing

About the Author: John Dixon is a web developer and technical author. These days, John spends most of his time developing dynamic database-driven websites using PHP and MySQL.

Go to to view one of John's sites. This site contains articles and photos relating to the history of the computer.

To find out more about John's work, go to

Your Article Search Directory : Find in Articles

© The article above is copyrighted by it's author. You're allowed to distribute this work according to the Creative Commons Attribution-NoDerivs license.

Recent articles in this category:

Most viewed articles in this category: