PERL my getopt command line parser

17 July 2011

Usually I want my PERL scripts to work on all machines that have PERL installed. Now a script usually parses the command line, in perl you have Getopt::Std to does that for you. Now this command line parser is in a core module, however this does not mean that it is available on each PERL installation, for example the default installation of RedHat Enterprise Server installs PERL without the core modules; so we can’t use Getopt::Std for parsing command lines here.

Fortunately we can roll our own in 30 lines, so there is no problem in copying this function over into other scripts; as we all know copying code is an awful engineering practice, that should be punished by decapitation of the offender (in most cases the offender is acting without a head to begin with, so this measure becomes impractical ;-) That does not mean that we should Screw good practices - punishment is never far behind

Here we can justify copy-paste by our desperate goal of running a PERL script on all installations. Now here is the script As you can see, the getopt function receives an array, where each entry describes an option, this simplifies the function to the extent that we don’t have to parse the argument specification. Otherwise it is all quite similar to the original Getopt::Std

almost ten years later.

Nowadays perl5 is not quite used as a scripting language; in a corporate environment that is something that is frowned upon. Meanwhile they even added hash tables to bash, so that is supposed to be the way to go. (doing tasks that shell out to programs and do some text processing is less practical in python)

Now those who didn’t like perl5 got it much worse - bash is such an awful language…. (at least it has tracing in the form of set-x)