Fieldholders in Perl
Perl stands for Practical Extraction and Report Language and allows us to format data so it is easy to read. Perl uses the format and write keywords to do the formatting. In this article, we will go through the field holders for the formatting. To learn more about format and write, go to this article.
Text fields
Field holders defines what type of data will be held in a given field. There are 4 types of fields available in Perl, text, numeric, filled, and multiline. In the case of text, field holders also holds the formatting of the field, i.e. left justified, right justified and centered. Most field holders start with @ and the characters following the @ indicates the field type. The number of characters, including the @ indicates the field width. If the field width is longer than the data, the value gets padded depending on the formatting of the field. If the data is longer than the field width, it is automatically truncated.
format STUDENT = @<<<<< @||||| @>>>>> $firstName $middleName $lastName . select(STDOUT); $~ = STUDENT; @firstNames = ("Jonathon", "Jane", "Joe"); @middleNames = ("Robert", "Doe", "B"); @lastNames = ("Doe", "Smith", "Jones" ) ; @ages = (11,15, 8); @grades = (5 , 10, 3); $i = 0; foreach (@firstNames) { $fName = $_; $lName = $middleNames[$i]; $lName = $lastNames[$i]; $i++ ; write; } The above code will output the following: Jonath Robert Doe Jane Doe Smith Joe B Jones
Numeric fields
Another kind of fieldholder is a fixed-precision numeric field, useful for financial reports. This field also begins with @, and is followed by one or more #'s with an optional dot indicating a decimal point. Once again, the @ counts as one of the characters of the field. Perl doesn't provide floating currency symbols or brackets around negative values. To do so, we will have to write our own subroutine.
format ACCOUNT = Start balance: @#####.## Widthdrawal: @#####.## Balance: @#####.## $start_balance, $debit, $start_balance-$debit .
Multiline fields
Perl normally stops at the first newline of a value when placing the result into the output. Perl provides the multiline fieldholder to allow us to include a value that may have many lines of information. This fieldholder is marked by @* on a line by itself.
format STDOUT = @* @string . $string = "John Jane Richard Harry "; write;
Filled fields
Another kind of fieldholder is a filled field. This fieldholder allows us to create a filled paragraph, breaking the text into conveniently sized lines at word boundaries, wrapping the lines as needed. First, a filled field is denoted by replacing the @ marker in a text fieldholder with a ^, so we get ^<<<, for example. The corresponding value for a filled field on the following line of the format must be a scalar variable containing text, rather than an expression that returns a scalar value including a single scalar element of an array or hash. The reason for this is that Perl will alter the variable while filling the filled field. When Perl is filling the filled field, it takes the value of the variable and grabs as many words as will fit into the field. These words are actually taken out of the variable. The value of the variable after filling this field is whatever is left over after removing the words. This isn't much different from how a normal text field works. We're printing only as much as will fit except that we're respecting a word boundary rather than just cutting it off at the field width. The beauty of this filled field appears when we have multiple references to the same variable in the same format.
format REVIEW = Name: @<<<<<<<<<<<<< Comment: ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< $movieName, $comment ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< $comment ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< $comment ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< $comment .