Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

nkuitse (193)

nkuitse
  (email not shown publicly)
http://www.nkuitse.com/

Journal of nkuitse (193)

Thursday December 16, 2004
01:52 PM

xor virgin no more

[ #22335 ]

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...

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.