.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.42) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "boolean 3pm" .TH boolean 3pm "2022-10-13" "perl v5.34.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" boolean \- Boolean support for Perl .SH "VERSION" .IX Header "VERSION" This document describes boolean version \fB0.46\fR. .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use boolean; \& \& do &always if true; \& do &never if false; \& \& do &maybe if boolean($value)\->isTrue; .Ve .PP and: .PP .Vb 1 \& use boolean \*(Aq:all\*(Aq; \& \& $guess = int(rand(2)) % 2 ? true : false; \& \& do &something if isTrue($guess); \& do &something_else if isFalse($guess); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Most programming languages have a native \f(CW\*(C`Boolean\*(C'\fR data type. Perl does not. .PP Perl has a simple and well known Truth System. The following scalar values are false: .PP .Vb 5 \& $false1 = undef; \& $false2 = 0; \& $false3 = 0.0; \& $false4 = \*(Aq\*(Aq; \& $false5 = \*(Aq0\*(Aq; .Ve .PP Every other scalar value is true. .PP This module provides basic Boolean support, by defining two special objects: \&\f(CW\*(C`true\*(C'\fR and \f(CW\*(C`false\*(C'\fR. .SH "RATIONALE" .IX Header "RATIONALE" When sharing data between programming languages, it is important to support the same group of basic types. In Perlish programming languages, these types include: Hash, Array, String, Number, Null and Boolean. Perl lacks native Boolean support. .PP Data interchange modules like \s-1YAML\s0 and \s-1JSON\s0 can now \f(CW\*(C`use boolean\*(C'\fR to encode\fIdecode\fRroundtrip Boolean values. .SH "FUNCTIONS" .IX Header "FUNCTIONS" This module defines the following functions: .ie n .IP """true""" 4 .el .IP "\f(CWtrue\fR" 4 .IX Item "true" This function returns a scalar value which will evaluate to true. The value is a singleton object, meaning there is only one \*(L"true\*(R" value in a Perl process at any time. You can check to see whether the value is the \*(L"true\*(R" object with the isTrue function described below. .ie n .IP """false""" 4 .el .IP "\f(CWfalse\fR" 4 .IX Item "false" This function returns a scalar value which will evaluate to false. The value is a singleton object, meaning there is only one \*(L"false\*(R" value in a Perl process at any time. You can check to see whether the value is the \*(L"false\*(R" object with the isFalse function described below. .ie n .IP """boolean($scalar)""" 4 .el .IP "\f(CWboolean($scalar)\fR" 4 .IX Item "boolean($scalar)" Casts the scalar value to a boolean value. If \f(CW$scalar\fR is true, it returns \&\f(CW\*(C`boolean::true\*(C'\fR, otherwise it returns \f(CW\*(C`boolean::false\*(C'\fR. .ie n .IP """isTrue($scalar)""" 4 .el .IP "\f(CWisTrue($scalar)\fR" 4 .IX Item "isTrue($scalar)" Returns \f(CW\*(C`boolean::true\*(C'\fR if the scalar passed to it is the \f(CW\*(C`boolean::true\*(C'\fR object. Returns \f(CW\*(C`boolean::false\*(C'\fR otherwise. .ie n .IP """isFalse($scalar)""" 4 .el .IP "\f(CWisFalse($scalar)\fR" 4 .IX Item "isFalse($scalar)" Returns \f(CW\*(C`boolean::true\*(C'\fR if the scalar passed to it is the \f(CW\*(C`boolean::false\*(C'\fR object. Returns \f(CW\*(C`boolean::false\*(C'\fR otherwise. .ie n .IP """isBoolean($scalar)""" 4 .el .IP "\f(CWisBoolean($scalar)\fR" 4 .IX Item "isBoolean($scalar)" Returns \f(CW\*(C`boolean::true\*(C'\fR if the scalar passed to it is the \f(CW\*(C`boolean::true\*(C'\fR or \&\f(CW\*(C`boolean::false\*(C'\fR object. Returns \f(CW\*(C`boolean::false\*(C'\fR otherwise. .SH "METHODS" .IX Header "METHODS" Since true and false return objects, you can call methods on them. .ie n .IP """$boolean\->isTrue""" 4 .el .IP "\f(CW$boolean\->isTrue\fR" 4 .IX Item "$boolean->isTrue" Same as isTrue($boolean). .ie n .IP """$boolean\->isFalse""" 4 .el .IP "\f(CW$boolean\->isFalse\fR" 4 .IX Item "$boolean->isFalse" Same as isFalse($boolean). .SH "USE OPTIONS" .IX Header "USE OPTIONS" By default this module exports the \f(CW\*(C`true\*(C'\fR, \f(CW\*(C`false\*(C'\fR and \f(CW\*(C`boolean\*(C'\fR functions. .PP The module also defines these export tags: .ie n .IP """:all""" 4 .el .IP "\f(CW:all\fR" 4 .IX Item ":all" Exports \f(CW\*(C`true\*(C'\fR, \f(CW\*(C`false\*(C'\fR, \f(CW\*(C`boolean\*(C'\fR, \f(CW\*(C`isTrue\*(C'\fR, \f(CW\*(C`isFalse\*(C'\fR, \f(CW\*(C`isBoolean\*(C'\fR .SH "DEPRECATIONS" .IX Header "DEPRECATIONS" This module offered an export tag, \f(CW\*(C`\-truth\*(C'\fR, that overrides the Perl interpreter's internal values for true and false. This has been found to corrupt the interpreter in some circumstances. Also, these overrides will no longer be possible as of Perl 5.22. Therefore, the \f(CW\*(C`\-truth\*(C'\fR import tag is deprecated. .SH "JSON SUPPORT" .IX Header "JSON SUPPORT" JSON::MaybeXS (or less preferably \s-1JSON\s0.pm ) will encode Perl data with boolean.pm values correctly if you use the \f(CW\*(C`convert_blessed\*(C'\fR option: .PP .Vb 4 \& use JSON::MaybeXS; \& use boolean \-truth; \& my $json = JSON::MaybeXS\->new\->convert_blessed; \& say $json\->encode({false => (0 == 1)}); # Says: \*(Aq{"false":false}\*(Aq, .Ve .SH "AUTHOR" .IX Header "AUTHOR" Ingy döt Net .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" Copyright 2007\-2016. Ingy döt Net. .PP This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. .PP See