Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a built-in true/false boolean value in Perl? [duplicate]

Tags:

perl

Possible Duplicate:
How do I use boolean variables in Perl?

[root@ ~]$ perl -e 'if(true){print 1}'
1
[root@ ~]$ perl -e 'if(false){print 1}'
1

I'm astonished both true and false passes the if...

like image 500
asker Avatar asked Aug 04 '11 04:08

asker


People also ask

Does Perl have true and false?

In most of the programming language True and False are considered as the boolean values. But Perl does not provide the type boolean for True and False. In general, a programmer can use the term “boolean” when a function returns either True or False.

Does Perl have boolean?

Perl does not have a special boolean type and yet, in the documentation of Perl you can often see that a function returns a "Boolean" value. Sometimes the documentation says the function returns true or returns false.

Which of the statement is false in Perl?

What is true/false in Perl. In short, the following elements evalue to false in Perl: The number zero (0) means false. The string zero ('0' or "0") means false.


1 Answers

Always use warnings, especially on one-liners.

Perl has no true or false named constants, and without warnings or strict enabled, a "bareword" (something that could be a constant or function but isn't) is silently interpreted as a string. So you are doing if("true") and if("false"), and all strings other than "" or "0" are true.

like image 199
ysth Avatar answered Sep 24 '22 06:09

ysth