boolean madness: false is the new true?

S

Steve Vertigan

I have the following method in a module I'm writing...

sub uses {
my $self = shift; my $var = shift;
if ($var) {
if ($self->{'DEFINED'}{$var}) { return true; }
else { return false; } #this line broken
} else {
my @def = keys %{$self->{'DEFINED'}};
return @def;
}
}

and using it in the context if ($obj->uses('badvar')) always tests as
true. However using return 0 instead of return false seems to
work fine. Either my perl interpreter is broken or my brain is, any
guesses?
 
J

Jürgen Exner

Steve said:
I have the following method in a module I'm writing...

sub uses {
my $self = shift; my $var = shift;
if ($var) {
if ($self->{'DEFINED'}{$var}) { return true; }
else { return false; } #this line broken
} else {
my @def = keys %{$self->{'DEFINED'}};
return @def;
}
}

and using it in the context if ($obj->uses('badvar')) always tests as
true. However using return 0 instead of return false seems to
work fine. Either my perl interpreter is broken or my brain is, any
guesses?

When trying to run your code isn't the warning message from perl quite
obvious:

Bareword "true" not allowed while "strict subs" in use at t.pl line 6.
Bareword "false" not allowed while "strict subs" in use at t.pl line 8.
Execution of t.pl aborted due to compilation errors.

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top