I'll explain the Perl syntax in Listing 1 enough so that you can understand what the script is doing. If you want to learn more, Learning Perl on Win32 by Randal L. Schwartz, Erik Olson, and Tom Christiansen ((O’Reilly & Associates) is an easy-to-read book that explains how to write simple and complex scripts. To get started with Perl, just copy two files (perl.exe and perl100.dll) from the Microsoft Windows NT Resource Kit.

In Perl, scalar variables (variables that can hold only one item) start with $, and arrays (or lists) start with the @ symbol. The first line of Listing 1 takes arguments from the command line. If you type

perl rule8020.pl 20000111.log Compaq

at the command line, you can access all the text after the Perl script file name (rule8020.pl) through the @ARGV list. This Perl construct stores the text 20000111.log into the $tracking_file variable and the word Compaq in the $comment variable.

Line 3 opens the file so you can read the tracking log data lines. In line 4, reads each line from the file and stores the text into the special system variable $_. Lines 4 - 9 count the lines of tracking data that you’ll use to determine the ratios. After the script has read the whole file, it continues processing at line 10 to close the file, perform some calculations (lines 12 - 15), and then print the report (lines 17 - 22).

Line 5 is probably the most confusing part of the program. Line 5 splits the tracking data into individual fields that you can evaluate as elements in a list or array. To understand this process, let’s dissect line 5

@tracking_data = split(/\t/, $_);  $_

where @tracking_data is the name of the array (i.e., list) that stores the results of the split operation and $_ is a system variable. After the read, () $_ contains a text string something like that in Figure A.

The split statement in line 5 is a Perl function that you use to separate a string into chunks based on a specific character or characters used as delimiters. In this case, the tab character separates the tracking log data into individual fields. The tab is represented by the \t in the syntax. The two slash characters are part of split's syntax and must surround the delimiter.

Again, if you assume that the line that the script reads from the tracking log contains the example text, then after the split, the script organizes the tracking data like that in Figure B. You can see in lines 7 and 8 of Listing 1 that you reference the tracking log event number stored in the \[1\] array element. The line contains a $ prefixing the variable name tracking_data instead of an @ symbol. When you reference the array as a whole, you use the @ symbol; when you reference individual scalar elements of the array with the \[ \] notation, you use a $ symbol.

Lines 7 and 8 have another Perl syntactical feature. You can use the if flow control statement after an operation. These lines then mean, "Increment the variable if the condition that follows is true," instead of the more traditional "If the condition is true, then increment the variable."

Perl can look complex and intimidating, but it can often do complex tasks with just a small amount of code. When you understand its syntax, Perl will make a powerful addition to your systems administration tool set.