It looks like you want something that is true or false when used in boolean context (meaning, && and || work as expected) and is the string "true" or "false" when used in string context. Do note that perl doesn't have a specific boolean type, but uses either strings or numbers instead. You can obtain what (I think) you want with an overloaded object. I got lazy so what I did is use the code of boolean and replaced the "overload" call with:

use overload '0+' => sub { ${$_[0]} }, '""' => sub { ${$_[0]} ? "true" : "false"}, '!' => sub { ${$_[0]} ? $false : $true }, fallback => 1;
Then the return value of true and false can be used a string. There are some possible issues though, the string "false" is not empty, so it is actually true, and the string "true" turns into the number 0 so it will be different from the number you may get from true() (which is 1):
use boolean; $\ = $/; my $false = false; print "\$false is $false" unless $false; my $copy = $false; print '$copy is also false' if not $copy; my $str_false = "$false"; print '$str_false is true?' if $str_false; print 'But equal to false' if $str_false eq $false; my $true = true; my $str_true = "$true"; print '$str_true is true but != from true' if $str_true and ($str_true + != true); print "The value of this test is: ", boolean(true||false);
$false is false $copy is also false $str_false is true? But equal to false $str_true is true but != from true The value of this test is: true
It kind of works as long as you do not try to convert the values to a string or a number, and then use them again as boolean. The best solution IMHO would rather be to translate the value to the boolean string whenever you want to print it, and keep the number otherwise, with sub to_bool_str { $_[0] ? "true" : "false" }; print "1 is ", to_bool_str (1);


In reply to Re: Evaluate arbitrary boolean expressions by Eily
in thread Evaluate arbitrary boolean expressions by gauss76

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.