SHORT QUESTION
What are the best ways to represent true and false consistently in libraries of Perl code?
1
/0
?1
/ the special empty string that Perl's native boolean operators
return?undef
?()
(i.e. the empty list)?
QUESTION BACKGROUND
We all know that Perl is very flexible with regard to booleans, as with most things.
For example, Perl treats the following as false: undef()
, the number 0
(even if written as 000 or 0.0), an empty string, '0'
(a string containing a single 0 digit). Perl treats the following as true: any other scalar values, 1
, -1
, a string with a space in it (' '
), '00'
multiple 0s in a string, "0\n"
(a '0' followed by a newline), 'true'
, 'false'
, 'undef'
, etc. Plus, array-to-scalar and list-to-scalar conversions mean that you can often get away with using an empty array as a false value. (Credit to http://perlmaven.com/boolean-values-in-perl for the partial list of scalar values accepted as true or false by Perl.)
But given all of these values that are treated as true or false, what should a Perl boolean function return?
1/0
-1/0
(nah)1/undef
1/""
If you use any of these conventions, you will quickly find that the code that you write does not behave as nicely as normal Perl code. You will not be able to replace $a<$b
by custom_less_than($a,$b)
and have it work exactly the same.
Consider:
> perl -e 'use warnings;
sub my_eq { return ( $_[0]==$_[1] ? 1 : 0 ) }
print "compare (1==0) <<".(1==0).">>"
." to my_eq(1,0) <<".my_eq(1,0).">>"
."\n" ;'
Output:
compare (1==0) <<>> to my_eq(1,0) <<0>>
What is the best known method for returning values from boolean functions in Perl when you are writing them yourself?
Perhaps you want your code to be Perl-like, potentially substitutable for Perl's existing boolean operators. Or perhaps you want numeric values like 1/0. Or perhaps you come from LISP, and expect Perl's undef
to be used LISP's nil
for false (but then you trip upon Perl's implicit treatment of many other values as false).
---+ SHORT ANSWER
Here are two self-consistent schemes for Booleans in Perl:
1/0
- printable and portable1/!!0
- most like Perl's native boolean functions1/0
is probably most familiar to programmers from other languages, like C or Python. You can print1/0
Boolean values, add them, and so on. But... Perl's native Boolean operators are not1/0
values,return $a<0
is NOT the same asreturn 1 if $a<0; return 0
.1/0!!
is my attempt to create an abbreviated name for the scheme that Perl's native Boolean operators use: 1 for true, an empty string that is specially marked so that it does not produce warnings when used in arithmetic or interpolated into strings.!!0
is one of the easiest ways to produce this special Perl false value, and should be familiar to programmers in many languages like C as a way of standardizing Boolean values. You can add1/0!!
, and you can print1/0
Boolean values, so long as you don't care if the false value may be invisible, i.e. an empty string.Avoid accidentally mixing numeric 1/0, Perl's native conditions,
undef
, and other schemes in the same function or library.When deliberately mixing, use conversion operators such as
!!$bool_0or1
to convert to a traditional1/!!0
Perl value(0+($a<0))
to convert a Perl relational result to1/0
(!!any_other_boolean())
(0+any_other_boolean())
($other_bool?1:())
to convert to1/()
from the other boolean schemes($other_bool?1:undef)
to convert to1/undef
from the other boolean schemesQ: are there any shorter or prefix-ish notations, other than
?:
?There are some more possible schemes
1/()
- more precisely, return 1 or nothing -- which can catchsome Perl-ish errors like returning a boolean scalar false like 0 or undef in a list context, where it would become true
(1)/(0)
- return a list of length 1 conrtaining 1, or an empty list. Similar to1/()
, consistent in the sense that both true and false are arrays.1/undef
- another possibility, catches errors that1/()
return 1 or nothing may causeI hesitate to recommend these. At the very least,
1/()
is inconsistent in that But... they certainly have been used by Perl programmers, so you should be prepared to deal with code that uses these schemes. I.e. be prepared to debug bugs caused by these schemes.1/()
is my attempt to create an abbreviated name for a scheme where functions that return truereturn 1;
, and functions that return false doreturn;
, i.e. a return with no operand value. I.e. return 1 or nothing. I believe that return nothing is equivalent toreturn ();
. This scheme protects you against bugs caused by a programmer evaluating your function in a list context rather than a scalar context. But it exposes you to bugs such as{1=>return_nothing_if_false(),2=>return_nothing_if_false()}
(snce you probably do not want{1=>2}
.BTW I think that it might be more consistent to do the scheme
(1)/()
. This would allow you to consistentl;y have variables of this Boolean type, admittedly@array
variables.Note that
1/undef
is NOT equivalent to any of the above.1/undef
Boolean variables give warnings when false=undef
is printed or interpolated or used in arithmetic, but not when true=1 is so-manipulated, and evaluates to true in a list context. One might be tempted to say that it has the worst features of all the schemes.I hesitate to hesitate these schemes
1/()
,(1)/()
, or 1/undef. At the very least,1/()
is inconsistent in thatAll three of these schemes
1/()
,(1)/()
, or 1/undef expose you to bugs such as{1=>f1_return_nothing_if_false(),2=>f2_return_nothing_if_false()}
, since you probably do not want{a=>"b"}
if both are false, and{a=>1,b=>1}
if both are true. At least if f1 returns true and f2 returns false, or vice versa, you will get a warning about odd sized hash.The programmer calling a function can control whether it is evaluated in list or scalar context, but she may not be able to control whether it returns true or false.
IMHO, if you do
1/()
,(1)/()
, or 1/undef you cannot safely call such functions in array context, like building keyword argumentsfoo(kw=>boolfunc(),...)
orfoo({kw=>boolfunc(),kw2=>...},...)
. Not without having to scatter !! or 0+ all over.---+ MEDIUM LENGTH ANSWER
Generalizing original answer:
Perl has many ways of representing truth; or, rather, Perl interprets many different values as true or false.
If you are creating a family of related functions, i.e,. a library, you are advised choose one of the following well-known schemes, and to use it consistently in your library:
Truth
1/0
- numeric - most portable to/from other languages, and more printableTruth
1/!!0
- most like standard Perl relational operators, less portable, less printable (unless you want false to be invisible)This answer emphasizes Boolean functions or methods, predicates. It is not trying to discuss non-Boolean functions, that return actual things like numbers or strings or refs - except briefly below.
@DaveCross suggests an additional interesting scheme
return 1
/return
nothing (almost1/()
, the empty list)a scheme that I remember from the early days of Perl - before refs, I think even before
undef
was a value that could be returned. But IIRC there are problems with this scheme and use warnings
, possibly also?:
, so I hesitate to recommend it fully until somebody explains better how to avoid such problems. Possibly usingwantarray
.---++ Choose 1/0 or 1/0!! (native Perl) and be consistent
I recommend that you choose one of these Boolean schemes, and use that scheme consistently.
The 1/0 boolean scheme is probably most portable to other languages.
The 1/!!0 scheme will make your code more closely resemble native Perl operators.
If you are using the
1/!!0
scheme, don't say "return 0", sayreturn !!0
.If you are using the
1/0
scheme, don't sayreturn $a < $b
, instead sayreturn 0+($a < $b)
If you are calling code that uses a different Boolean scheme (or, possibly, no consistent scheme), convert to the Boolean scheme that you use in your code, using operators such as
!!
to normalize a standard Perl1/0!!
Boolean0+
or1*
to convert to a more portable 1/0 boolean from a standard Perl 1/0!! boolean?:
and all the rest of Perl's arsenal for undefs and strings that may or may not want to be considered false or failuresIf looking at the return value of a function that returns a ref or undef
If
1/!!0
Perl-like Booleans, say return!!ref_retval_func()
ordefined ref_retval_func()
If
1/0
more portable Booleans, say return0+!!ref_retval_func()
or0+(defined ref_retval_func())
Way too much detail below.
---++ Possible?: return 1 or return nothing scheme (possibly 1/()?)
@DaveCross makes an interesting suggestion:
return 1 for a Boolean true value.
return nothing for a Boolean false value. That's because a bare
return will return an appropriate value depending on how the
subroutine has been called. The documentation for return says this:
If no EXPR is given, returns an empty list in list context, the undefined value in scalar context, and (of course) nothing at all in void context.
---++ Anti-Recommendation: do not mix Boolean schemes E.g., in the same function or library, do NOT do
in one place, and then somewhere else, or in later evolutions of the same code, do
I.e. choose one Boolean scheme, and be consistent. Mainly, this involves being consistent about the false value; to a lesser extent the true value.
---+ EXCESSIVE MESSY DETAIL
---++ Discussion elsewhere about Perl's many values of truth
Posts such as What do Perl functions that return Boolean actually return and Why does Perl use the empty string to represent the boolean false value? discuss what Perl boolean functions and operators actually return. Basically special values, whose behavior is specified by the Perl manuals.
@cim links to the perl manuals: http://perldoc.perl.org/perlsyn.html#Truth-and-Falsehood
Similarly http://perldoc.perl.org/perlop.html#Relational-Operators
Unfortunately, the accepted answer for What do Perl functions that return Boolean actually return discusses the internals, but then recommends
which is back to where we started.
@amon shows us the light (!!) in a comment on the question What do Perl functions that return Boolean actually return
There does not seem to be any literal for these special Booleans. There are, however, many ways of producing them:
(0<0)
,(0<1)
, etc.(!!1)
and(!!0)
are probably the nicest - especially since in some C/C++ programming circles they are used for a similar purpose. Plus,!!
can be applied to an incoming truth value to "normalize" it to this "Perl standard" boolean.---++ Anti-Recommendation: do not mix Boolean schemes E.g., in the same function or library, do NOT do
in one place, and then somewhere else, or in later evolutions of the same code, do
I.e. choose one Boolean scheme, and be consistent. Mainly, this involves being consistent about the false value; to a lesser extent the true value.
e.g. Avoid evolving code from
to
or
Coming to Perl from somewhere else, you may think that these are equivalent, and they mostly are, but if you do things like printing boolean return values in tests you will get differences.
If you evolve code from a Perl-ish boolean operator
evolve it to
If you want behavior to be as close to the same. Note the !!0 on the return of false, As far as I know, there is no simpler way to construct Perl's special return value for false.
Conversely, if you want to use the 1/0 Boolean scheme, them the original code should have been written as
---++ Creating a predicate from value / undef
Similarly, you may be tempted to take a function such as
and refactor it to a predicate
and then later evolve to, perhaps, have a sanity check or performance optimization.
This is neither 1/0 nor 1/!!0, and is not consistently value/undef either.
(Note: I am not saying this pre-check is a performance optimization --- but I am saying that performance optimizations might look like the above. Performance optimization is one of my specialties, and you want such optimizations to be refactoring. It sucks when optimized code performs better, but breaks in some of the places it is used. Hence my interest in code that performs as exactly like ... whatever it is replacing, like native Perl relational operators. Exactly means exactly.)
Instead do something like the following if you are using standard Perl-ish booleans.
or if you are using 1/0 Booleans
If instead of find_string_in_table it was find_object_ref_in_table, you might do just return
0+!!find_string_in_table(@_)
because you would not need to worry about strings likeq()
and"0"
.If you want boolean functions in code you write to behave like native Perl operators return (!!1) for true and (!!0) for false.
I.e. 0/1, but logically negate twice using the ! operator to convert your 1 or 0 into Perl's 'native' boolean.
e.g.
**---+ 0/1 --> 1/!!0 conversion **
If you consider !! as a conversion from "meta-boolean" to "special boolean",
consider 1* or 0+ as a conversion from special boolean to ordinary 0/1 boolean.
E.g.
print "test".(1*($a eq $b))."\n"
E.g.
print "test".(0+($a eq $b))."\n"
?: is more general, but more verbose.
---++ Non-Boolean error returns
This question and answer emphasizes Boolean functions or methods, predicates. It is not trying to discuss non-Boolean functions, that return actual things like numbers or strings or refs - except briefly below.
It is "nice" to have the return value extended to indicate special conditions such as failure, invalid input, etc., and which may be evaluated in the context of IF statements or other control flow such as and and or operators, typically to handle such errors, e.g. to provide default values.
We will limit our discussion of non-Boolean functions to this short list:
ref
/undef
: for functions that return a typical http://perldoc.perl.org/perlobj.html object, a ref to a blessed hash or other type. Returnundef
on error, not found, etc.any value /
undef
: for functions that return any type of value, scalar number or string, scalar ref whether blessed or unblessed.value/undef works best when
undef
is not a legitimate return value and can be problematic when undef is a legitimate value.n E.g. imagine an accessor function that returns the value of a hash field, $hash->{field} -- the field might legitimately have the value{ field => undef }
, so returning undef dfoes not distinguish between the field not existing and the field existing but having an undef value.GLEW personal opinion: since I write code that often needs to be ported to other languages, I prefer not to take advantage of Perl-specific tricks like
0+"0 but true"
."0E0"
at least is more portable, if you imagine that in some other language like C a functionconvert_string_to_float("0E0")
orconvert_string_to_int("0x0")
. I prefer"0x0"
because it looks special with the x, and0x0
is an integer value, whereas0E0
is interpreted as a float in some languages, so is more likely to give an error.---++ Possible?: return 1 or return nothing scheme (possibly 1/()?)
@DaveCross makes an interesting suggestion:
return 1 for a Boolean true value.
return nothing for a Boolean false value. That's because a bare
return will return an appropriate value depending on how the
subroutine has been called. The documentation for return says this:
If no EXPR is given, returns an empty list in list context, the undefined value in scalar context, and (of course) nothing at all in void context.
This is important as true and false values can differ subtly between scalar and list context. Imagine a subroutine like this: ...
@DaveCross goes on to show how returning any value other than the empty list results in loss of false-ness if a boolean function is evaluated in array context. Even
@array=(undef)
evaluates as true.I would like this scheme to work. I think that I used it years ago, in Perl 4 or earlier, but gave up on it when
use warnings
started becoming the thing to do.Insofar as I recall, I also had problems with conditional exprtessions ?: wigth this convention.
I have tried both "return;" and "return();"
Consider
---+ BOTTOM LINE
Use
1/0
(printable and portable),1/0!!
(most like Perl's native boolean functions).Possibly
return 1
orreturn
nothing, which is almost the same as1/()
. (But I have had problems with this approach.)Avoid mixing numeric 1/0, Perl's native conditions,
undef
, and other schemes in the same function or library.Finally, if you have ever done
you may have received
so it appears likely that some day Perl may have an "official" scheme for Booleans, with values true and false.
I wonder how those will behave?
I return 1 for a Boolean true value. I can't really see that
!!1
adds anything other than confusion.But I usually return nothing for a Boolean false value. That's because a bare
return
will return an appropriate value depending on how the subroutine has been called. The documentation forreturn
says this:This is important as true and false values can differ subtly between scalar and list context. Imagine a subroutine like this:
This works fine if it is called in scalar context.
All works as expected. But if you call it in list context, things go a bit weird.
In this case, the
else
block is never called. In list context, your subroutine returns a list containing a single scalar (the valueundef
) so@array
has a single element and theif (@array)
test is always true.Of course, your subroutine wasn't meant to be called in list context. But you can't control how other programmers will use your code.
But if the subroutine is written like this:
Everything will work as expected. If your subroutine is called in scalar context, it will return a scalar false value and if it is called in list context, it will return an empty list.
Returning an explicit false scalar value from a subroutine is, in my opinion, always suspect.
If you want to return an explicit scalar false value, then it's well worth checking the calling context and taking appropriate action if it's wrong.
Update: To answer this comment.
The job of a Boolean value is that you can ask it if it is true or false. That is all. In my opinion, you should not expect to be able to just print a Boolean value. The fact that you often can print Boolean values without triggering warnings is nice, but shouldn't be relied on. If I wanted to print a Boolean value, I would always use some kind of logic check (probably the ternary operator) to do it. So I'd write your example like this:
I'll just reiterate my original point. If you are returning any scalar value for false and there is any way that your subroutine can be called in list context (even accidentally) then you have introduced a potential bug into your code.
That, alone, is worth the trivial pain of having to decode Boolean values before printing them.