http://www.perlmonks.org?node_id=144086

I was working on a program the other day that performed a series of operations, all of which could be run individually and independently. Usually, all would be run together in batch mode, but for testing, it would be useful to be able to run different operations, or stages, individually.

So the idea was that in the absence of command line switches, all the operations would be performed. Otherwise, no operations would be performed, unless the correponding switch was explicitly stated on the command line. I find the construct has a certain elegance.

Naturally, a corresponding solution could be created with Getopt::Long, but I have to admit I have grown fond of Getopt::Mixed because the processing of command line switches happens more or less in your own code. YMMV.

It turns out that I now need to add switches in order to govern how certain stages operate, so the concept no longer holds, but I want to leave this idea here, should I ever need to come back to it.

use Getopt::Mixed; { Getopt::Mixed::init(' a b c d e alpha>a bravo>b charlie>c delta>d echo>e '); my $restricted = 0; my %go; while( my( $option, $value, $pretty ) = Getopt::Mixed::nextOption( +) ) { ++$restricted; $go{$option}++; } Getopt::Mixed::cleanup(); sub go { my $stage = shift; return 1 unless $restricted; $go{$stage} ? 1 : 0; } } go('a') and stage_a(); go('b') and stage_b(); ... sub stage_a { ... }

Oops, looking again at this in a new light, I realise that there is vestigial code that no longer serves any purpose. Specifically, the $restricted variable is redundant. An updated version could be written as

use Getopt::Mixed; { Getopt::Mixed::init(' a b c d e alpha>a bravo>b charlie>c delta>d echo>e '); my %go; while( my( $option, $value, $pretty ) = Getopt::Mixed::nextOption( +) ) { $go{$option}++; } Getopt::Mixed::cleanup(); sub go { 0 == scalar keys %go or defined( $go{$_[0]} ) } } go('a') and stage_a(); go('b') and stage_b(); ... sub stage_a { ... }