I don't want to tell you how long I banged my head on this until I remembered that the precedence of "or" and "||" differs in Perl... Here's my attempt to save someone similar frustration...
I was trying to use Text::PraseWords and my function wasn't working. Finally, I reduced it to this test program, very similar to the example in the documentation:
#!/usr/bin/perl -w
use Text::ParseWords;
@fields = &parse_line(',', 0, q{foo,bar,baz}) || die "siteindex is corrupt!\n";
$i = 0;
foreach (@fields) {
print "$i: <$_>\n";
$i++;
}
Instead of printing "0: foo", "1: bar", etc. as expected, I got just "0: 3". I quickly figured out that this was the size of the array, and proved it by changing my test string, but why was this happening? After ruling out the delimiter, I knew it had to have something to do with the die
statement...
Of course, I immediately thunked myself in the forehead, realizing that my habit of using ||
instead of or
had just gotten me into trouble. Because ||
has a high precedence, @fields was getting assigned the result of the logical comparison, rather than the array returned by parse_line.
This variant works perfectly:
#!/usr/bin/perl -w
use Text::ParseWords;
@fields = &parse_line(',', 0, q{foo,bar,baz}) or die "siteindex is corrupt!\n";
$i = 0;
foreach (@fields) {
print "$i: <$_>\n";
$i++;
}
It's those little things we "know" but forget that come back to bite us, isn't it? Having whipped off some cool scripts recently, I was confident that I wasn't doing anything dumb, until I was forced to prove otherwise. Pride goeth before destruction, I guess.
Syndicate Me via RSS!
(Instructions)
Copyright 2003-2009 Robert Szarka
Powered by Blosxom!