mistaker has asked for the wisdom of the Perl Monks concerning the following question:

What do you reckon, what is the best way to implement C++ or Pascal style boolean data type in Perl?
I use enum module for this.
use enum qw(FALSE=0 TRUE=1);
Does it have some drawbacks I should be aware of? Don't know too much about enum module, opinions welcome.

Definition from Camel book says any string except for "" and "0", any number except for 0 and any reference are true, any undef value is false. So my enum seems to fulfill this definition, I am just curious whether the enum can misbehave in some situations...

-- Jan Stoklasa - "Mistaker" --
-- For those about to Perl, we salute you. --

Replies are listed 'Best First'.
Re: C++ style boolean type in Perl?
by japhy (Canon) on Sep 28, 2000 at 21:22 UTC
    Um, there's no REAL need for explicit "true" and "false" datatypes in Perl. Watch:
    if (foo()) { foo() returned a true value } if (!bar()) { bar() returned a false value } $DEBUG = 1; if ($DEBUG) { ... }
    If you really feel the need for 'true' and 'false', then please make this compromise with me:
    use constant TRUE => 1; use constant FALSE => 0; # ... $DEBUG = TRUE; # ... if ($DEBUG) { ... }
    Please do NOT test for equality with TRUE or equality with FALSE. They are pointless test, and broken in cases of TRUE that aren't '1'.

    $_="goto+F.print+chop;\n=yhpaj";F1:eval
RE: C++ style boolean type in Perl?
by indigo (Scribe) on Sep 28, 2000 at 22:06 UTC
    Mostly it is useless, and it is potentially dangerous if you are used to C++ booleans. C++ has only one false value, so:
    if (x == FALSE)
    will work every time. However, in Perl you have:
    if ($x == $false) if ($x eq $false)
    How do these work? What if $x = 0? or ''? or undef or 0.0? Are you sure? Perl has mutliple false values, so switching to a single false value is just asking for trouble.
    Perl is perl, don't try to redefine the language. It is like the Pascal programmer who starts all his C code with:
    #define BEGIN { #define END }
    Server no purpose, confusing to others, and it just begging for weird ass bugs.
      IMHO, adding these 2 constants in Perl is not redefining the language. The resulting script still looks like Perl. I've use these 2 constants in my scripts and I think using them enhances readability.
Re: C++ style boolean type in Perl?
by AgentM (Curate) on Sep 28, 2000 at 21:50 UTC
    I agree with japhy, but even constants bloat the code in the simple case of constants. That's some type of function call for every constant.
      No, it isn't. When you use constant, Perl replaces the constants with the values they return at compile-time.

      $_="goto+F.print+chop;\n=yhpaj";F1:eval