Tech Tip of the Day: Perl’s system function
If you are writing automation in Perl (very common), you will certainly need to interact with the system. You have multiple ways to implement system interaction in Perl; one of the most efficient & effective methods is use of the system function.
Background:
You need to create multi-language installers at the end of your build process. Each installer must have localized versions of messages, error codes and actual content. You are producing several installers per build, and this must be automated. You are creating a SuperInstaller script which will produce all localized installers.
Once you’ve selected your installer technology (another post…), you can then begin to build your wrapper script around the Perl module you’ve built (yes, another post…) for production of any given installer. The wrapper, which we are discussing today, iterates processing over a set of files that provide input to a routine that will produce localized versions of the installers.
In the example below, we are interating over the languages by processing each through the ‘prepinstaller.pl’ script. This script (whose content is not important for this discussion) simply does the localization for the installer and drops the now language-appropriate files into the directory structure that the installer software uses to create new installers. This process, which we launch from the SuperInstaller script, uses the system call to execute the code that we have passed into it from the args array, which itself is composed of the fully-qualified path of the perl binary, the path & name of the script we want to run, as well as the two command line arguments required by the prepinstaller.pl script: language and target. (target in this case refers to Release or Debug, which in installer creation parlance means a big difference).
You can use the exact same set of code for every language; just iterate over it:
my ($language, $target) = @_;
@args0 = ("/usr/bin/perl", "./prepinstaller.pl", "language=$language", "target=$target");
system(@args0) == 0 or die "system @args0 failed: $?";
If the script fails, you will get a well-formed error message that includes the arguments passed to the script. This is very handy when debugging enhancements to the script – did the script itself fail or did our automation fail to construct an executable command line? This reduces debugging time to minutes.
Tags: freepository, automation, installer, perl, tech-tip
Popularity: 1% [?]