Internet

Beginning CGI Programming with Perl

Examining the flastmod Command

This Tutorial started out with an example of the flastmod command. That was a pretty simple example to begin with, but the following example, although no more complex, illustrates the utility and power you can get with the simple flastmod command.

Note

The name flastmod uses a standard UNIX command-naming trick. It is not meant to confuse you. The command name is constructed to help you figure out the type of command it is and what it does. The f in flastmod stands for file; last, of course, is last; and mod stands for modified. Lots of UNIX, Perl, and C commands begin with f to indicate that they operate on files. So the command really says operate on a file and return its last modified date.

You can use the flastmod command to let everyone know that your Web page has been updated recently, or you can use it to identify the latest changes to each portion of your Web page. The following Web page uses the include command and the flastmod command to tell the reader when an article was last updated. I like this a lot more than the “new” images that have cropped up on the Net. This way, your Web visitor will know what is new to her, and you don’t have to modify the main file each time you add a new article. If you’re building an electronic newspaper, as illustrated in Figure 3.4, this is an excellent way to let your readers know which articles they have changed.

Figure 3.4 : Including the date the article was written using SSIs.

The HTML for this page does not contain any of the articles on the page. The HTML in Listing 3.4 is just a template for an electronic newspaper with the include directive for each article to be added.


Listing 3.4. The electronic newspaper template HTML.

01: <html>02: <head>03: <title>An eletronic paper </title>04: </head>05: <body>06: <h4>This paper was generated on <!--#config timefmt="%x" -->07: <!--#echo var="DATE_LOCAL" -->, by including the following articles.  </h4>08: Each article has the date it was last modified.09: <!--#include file="epaper-include-files/article1.shtml" -->10: <!--#include file="epaper-include-files/article2.shtml" -->11: <!--#include file="epaper-include-files/article3.shtml" -->12: <hr>13: The electronic paper main file was last modified on <!--#flastmod file="epaper.shtml" -->.14: </body>15: </html>

Notice on line 6 the setting of the date format using the config command. What’s interesting here is the different date formats in Figure 3.4. The config command is supposed to affect all the SSI commands that print any type of date. It worked for line 7, where the date when the e-paper was compiled is printed. And it worked on line 13, where the date of the template is printed. Why didn’t it work for the included files? Listing 3.5 shows one of the included files and the answer to the question.


Listing 3.5. An included e-article, with the flastmod command embedded in it.

1: <p>2: If you use this technique to build an e-paper just include the flastmod3: in every new file you add to your epaper.4: <p>5: <em>6: This article was last modified on <!--#flastmod file="article2.shtml" -->.7: </em>

Note the flastmod command on line 6. Because the command is in a separate file, it is not affected by any previous commands from other SSI files. This works for two reasons. First, you can nest SSI files. The e-paper is an example of that type of nesting. The e-paper template is an SSI file, and each article is an SSI file. Second, when the included SSI file is parsed, the server ignores any previous config format commands. The server parses the file looking for SSI commands, and because this file doesn’t set the date format anywhere, the server uses the default format shown in Figure 3.4 below each article.

If this method of building your e-paper proves to be too slow, try moving the location of the flastmod command. Remember that it takes longer to parse files, and all SSI files must be parsed. If you move the flastmod SSI command and its formatting commands to the e-paper template, the articles themselves can be straight HTML files. The server won’t have to parse the article files, and that should speed up the loading of the entire e-paper a bit.

The flastmod command has basically the same syntax as the include command. It accepts two command arguments: virtual and file. And virtual and file have exactly the same meaning for the flastmod command as for the include command. The virtual command argument defines the path to the file from the document root, and the file command argument defines the path to the file relative to the current directory.

Take note of how the relative pathname works. If you look at lines 9-11 of Listing 3.4, you will notice that the included articles are in a subdirectory of the e-paper template. But on line 6 of Listing 3.5, the file command is used without indicating any directory. So when the server parses the included file and executes the flastmod command, it looks in the current directory. The server has changed directories! While the server is parsing the included articles, the current directory is the directory in which it finds the included file. In this case, this is one subdirectory below the e-paper template: the epaper-include-files directory.

This is one reason why you might want to use the virtual command argument. If you are including other files in your SSI files, when you move one file, you will have to move or copy every file that you have included. If you use the virtual command, which gives the full pathname to the file, you will only have to change any references to the file you are moving.