There's a rosy glow in my cheeks as I write this, for today I am an xor virgin no more.
That's right; I've just used Perl's xor operator for the first time, and it was a thrill!
The story of hoy
hoy (hack on yaml) is a script I wrote that lets me manipulate YAML documents from the command line. I can do things like create hashes and arrays:
$ hoy array </dev/null
--- #YAML:1.0 []
$ echo -e "name Yolanda P. Ipswich\nage 3" | hoy hash
--- #YAML:1.0
age: 3
name: Yolanda P. Ipswich
Fetch values from a YAML hash:
$ hoy get -p name -i ulysses.yaml
Ulysses K. Fishwick
...and so on. Each action is specified as a subcommand: hoy array, hoy foreach, etc.
hoy is very handy for what I do, and I love to say hoy. (hoy! hoy! hoy!)
The deed itself
While making a few changes in hoy today, I realized that after reading options from the command line, subcommands that don't take any non-option arguments were ignoring extra, unused arguments. That's not good, so I added a hash %takes_arguments:
my %takes_arguments = (
'foreach' => 1,
);
Then I added some code right after a call to GetOptions(...) that checks for argument "overflow":
exit usage()
if $takes_arguments{$cmd} and not scalar(@ARGV);
Then I realized I might as well check for argument "underflow" at the same time, and that's when it happened: the xor revelation (*blush*):
exit usage()
if $takes_arguments{$cmd} xor scalar(@ARGV);
Postscript
It took me a while to realize I should use xor; I first changed it to this:
exit usage()
if ($takes_arguments{$cmd} != 0) eq (scalar(@ARGV) != 0);
Only then did I realize I was on the cusp of something new and amazing. And now, things will never be the same...
xor virgin no more 0 Comments More | Login | Reply /