Thursday, October 20, 2005

Contextual::Return confusion

I had a short snippet of code using Contextual::Return something like this

use strict;
use warnings;

my $a = foo();
print "$a\n";

sub foo {
NUM { return 1234 }
STR { return "foo" }

but when I ran it, the output showed 1234 instead of foo. No matter what I did, $a was always the result of the first context I specified.

But, ah ha, I didn't actually use Contextual::Return. Adding the appropriate use Contextual::Return; line to the top of my snippet produced the correct behavior.

You would laugh if you knew how much time I spent debugging that confusion, but shouldn't there be a warning message or something since NUM and STR aren't defined? It seems so, but there wasn't.

No comments: