The Perl Command line to the Rescue
Filed under: miscellaneous
There are 0 comments on this article.
Introduction
Perl is currently one of the most widely used scripting languages. It is relatively straightforward to learn and great for creating basic but functional scripts. However, when needed, Perl also has the power and capabilities of a full blown procedural or object oriented language. As a tool, Perl can be of great use to build or release engineers. For example, you could write Perl scripts to extract software deliveries into specific directory structures, scripts to wrap processes around version control tools or scripts to extract text patterns for the automatic creation of release notes. Perl is particularly adept at just-in-time scripting, that is, one-shot scripts (usually initiated via the command line) to execute what might normally be considerable, time-consuming tasks. In the remainder of this article we will look at how we can invoke Perl from the command line and some examples of how to leverage its capabilities to achieve just that.
Standard Command Execution
To execute standard Perl commands from the command line, you can use the -e option followed by the line or lines of script that you wish to execute. For example, a rather straightforward command to print out some text would be:
command line test
Since the -e option is treated as if it ends with a newline, multiple -e options may be used. A more useful example to illustrate this, that displays the last accessed time of the file log.txt would be as follows:
-e "print scalar localtime((stat(\"log.txt\"))[9]);"
Last accessed time of log.txt:
Sun Sep 12 18:40:20 2004
Note here, the use of escaped quotes (\"), which you can use when you want to use the string quote character within an existing string.
Implicit Loops
Executing lines of Perl from the command line is one thing, probably of more use however, is Perl's ability to generate implicit loops. This feature is invoked by specifying either the -n or -p option, upon which Perl will automatically add loops around the code that you specify via the -e parameter discussed above. This feature can be extremely useful for parsing and processing files one line at a time - a common activity for build or release engineers. In practice, what this means is that when you specify the following code:
Perl will expand it to:
while (<>) {
your code
}
which will basically cause Perl to iterate over every line of file1 executing "your code". As an example, let us suppose that we had the following source code file HelloWorld.java:
public static void main (String args[]) {
System.out.print("Hello World ");
}
}
and that we wanted to extract all the class names for a summary report. We could use the following command:
class HelloWorld {
Here we are using $_ string, which in Perl refers to the current line. In this example, you might also want to do a bit of formatting at the same time, for example removing the trailing curly bracket ({) with the following code:
class HelloWorld
The -p option is similar to the -n option, except that it always prints the contents of $_ each time around the loop. Here is an example to display our source code but automatically line numbered:
1: class HelloWorld {
2: public static void main (String args[]) {
3: System.out.print("Hello World ");
4: }
If you need to have processing carried out either before or after the main code loop, you can also use a BEGIN or END block. Using this, we could improve our class listing example above to print out the total number of classes at the end as follows:
if (/class/) { $i++; s/{//; print $_ }" HelloWorld.java
class HelloWorld
Total classes: 1
In place Editing
One further ability worth mentioning is in-place editing, this allows you to make text translations (in-place) on a file. For example, referring to our HelloWorld.java code example above, if we wanted to replace all occurrences of Hello with Goodbye then we could use the -i option as follows:
Essentially what Perl is doing here is renaming the input file, then reading from this renamed version (whilst doing the translation) and writing to a new file with the original name. If -i is given a string argument, then that string is appended to the name of the original version of the file. If instead we specified the following:
then we would end up with the file HelloWorld.java.bak (with the contents of our original file) in the current directory. This feature is probably suitable for those with a nervous disposition or just beginning with their Perl skills.
Summary
This article was intended as an introduction to using the Perl command line and how it can help you simplify and automate your build and release engineering tasks. We concentrated here on the most widely used command line options, however there are a few more, so I encourage you to check out the references below.
