Analyzing the Program
In this step, we will analyze the Perl program on a line-by-line basis. Ensure that Line Numbers
are enabled in Komodo (View|View Line Numbers ). Ensure that the file "parse.pl"
is displayed in the Komodo Editor.
Line 1 - Shebang Line
- begins every Perl program
- associates the program with a Perl interpreter (in this case, the first one in the system's PATH variable)
- warning messages are enabled with the "-w" switch
Komodo Tip notice that syntax elements are
displayed in different colors. Configure syntax coloring in the
Preferences dialog. |
Lines 2 to 4 - External Modules
- these lines load external Perl modules used by the program
- Perl module files have a ".pm" extension; "use strict" uses the "strict.pm" module, part of the
core Perl distribution
- "use Text::CSV_XS" refers to the module installed in Step One
Top
Lines 6 to 7 - Open Files
- input and output files are opened; if the output file does not exist, it is created
- scalar variables, indicated by the "$" symbol, store the files
- "strict" mode (enabled by loading "strict.pm" in line 2) requires that variables be declared
using the format "my $variable"
Perl Pointer scalar variables store "single" items; their symbol ("$") is
shaped like an "s", for "scalar". |
Lines 9 to 13 - Print the Header to the Output File
- "<<" is a "here document" indicator that defines the string to be printed
- the text "EOT" is arbitrary and user-defined, and defines the beginning and end of the string
- the second EOT on line 13 indicates the end of output
- lines 10 and 11 are data that will be printed to the output file
Top
Lines 15 to 16 - Assign Method Call to Scalar Variable
- the result of the method call "new" is assigned to the scalar variable $csv
- the method "new" is contained in the module Text::CSV_XS
({binary => 1}) tells the method to treat the data as binary
Perl Pointer good Perl code is
liberally annotated with comments (indicated by the "#" symbol). |
Lines 18 to 19 - Method "getline"
- the method "getline" is contained in the module Text::CSV_XS, referenced in the $csv scalar variable
- "getline" reads the first line of mailexport.txt (referenced in the $in variable), parses the line into fields,
and returns a reference to the resulting array to the $fields variable
Top
Line 21 - "while" Loop
- the "while" statement is conditional
- the condition is "1', so the program endlessly repeats the loop because the condition is always met
- the logic for breaking out of the loop is on line 25
- the loop is enclosed in braces; the opening brace is on line 21, the closing brace on line 51
Komodo Tip Click on the
minus symbol to the left of line 21. The entire section of nested code
will be collapsed. This is
Code
Folding. |
Komodo Tip click the mouse pointer on line 21. Notice that
the opening brace changes to a bold red font. The closing brace on line 51 is displayed the same way. |
Lines 22 to 25 - Extracting a Line of Input Data
- the "getline" function extracts one line of data from the input file and places it in the $record scalar
variable
- if "getline" returns an empty array, the input file has been fully processed and the program exits the loop
and proceeds to line 52
Perl Pointer variable arrays store lists of items indexed by
number; their symbol ("@") is shaped like an "a", for "array". |
Top
Lines 27 to 31 - "foreach"
- "foreach" cycles through the elements stored in the @$record array
- the regular expressions on lines 29 and 30 find the characters "<" and "&", and replace them with
their character entity values ("<" and "&" are reserved characters in XML)
Top
Lines 33 to 35 - hash slice
- line 35 combines the @$record array with the field reference generated in line 19
Perl Pointer variable hashes are indicated by the symbol "%",
and store lists of items indexed by string. |
Top
Lines 37 to 50 - Writing Data to the Output File
- one line at a time, lines from the input file are processed and written to the output file
- portions of the data line (stored in the $record scalar variable) are extracted based on the
corresponding text in the field reference (the first line in the input file, stored in the $fields variable
Top
Line 51 - Closing the Processing Loop
- at line 51, processing will loop back to the opening brace on line 21
- the logic to exit the loop is on line 25
Lines 52 to 54 - Ending the Program
- line 52 prints the closing tag to the XML file
- lines 53 and 54 close the input and output files
|