[aklug] Re: perl doesn't natively do booleans?????

From: Lee <lee@afabco.org>
Date: Tue Aug 17 2010 - 17:15:11 AKDT

> Uh, you really can't have logic operations without boolean in any language.

Kind of my thinking. Hence my dumbstruck surprise.

> Give us an example of what you're trying to do and we can show you how to
> code it.

OK, here goes.

The specific case was predefining locals in a subroutine:

#!/usr/bin/perl

use strict;
use warnings;

<snip>
if (isValidIP($ip)) {
        push (@temphosts,"$ip\t$name");
        }
<snip>

sub isValidIP($) {

        my $isValid = false;
        
        if ($string =~ /(\d+)(\.\d+){3}/) { $isValid=true; }
        else { $isValid=false; };

<more subroutine processing here. Sets $isValid to true or false depending on whether
the thing in the ip turnes out to be a valid dotted quad or not.>

             return $isValid;

} # end isValidIP subroutine
   

This gave an error of:

Bareword "false" not allowed while "strict subs" in use at ./gen-hosts-file.pl line 85.
Bareword "true" not allowed while "strict subs" in use at ./gen-hosts-file.pl line 86.
Bareword "true" not allowed while "strict subs" in use at ./gen-hosts-file.pl line 92.
<more of same>

putting the

use constant false => 0;
use constant true => 1;

at the top of the program resolved the errors for this particular situation, but (as the
stackoverflow suggests) if doing it this way is a bad idea, I don't want to get into
that habit.

btw, I like your phrase "auto-typecasting".

I'm guessing my thinking was that since booleans are a single component variable,
they're classed as a scalar no different than other single component variables such as
strings or numbers. But I'm still having a problem with true and false not being native
keywords.

Thanks!

------------------------------------------

---------- Original Message -----------
From: Arthur Corliss <acorliss@nevaeh-linux.org>
To: Lee <lee@afabco.org>
Cc: aklug@aklug.org
Sent: Tue, 17 Aug 2010 16:33:16 -0800 (AKDT)
Subject: Re: [aklug] perl doesn't natively do booleans?????

> On Tue, 17 Aug 2010, Lee wrote:
>
> > Which just blows my mind. So obviously I'm missing something because that just makes no
> > sense. What am I missing? How do you do conditionals? Or do you have to explicitly do
> > the comparison? (answer is 'not so far, apparently') But to what?
> >
> > This stackoverflow just added to my confusion.
> >
> > http://stackoverflow.com/questions/1036347/how-do-i-use-boolean-variables-in-perl
>

> Perl does do boolean operations natively you just have to know what equates
> to boolean true/false given Perl's auto-typecasting feature.
>
> The answer on the page is correct. Like anywhere else non-zero is true,
> zero is false. Corner cases involving typecasts from null/strings/etc
> to numbers are mostly obvious.
>
> Give us an example of what you're trying to do and we can show you how to
> code it. You'll find Perl very succinct.
>
> --Arthur Corliss
> Live Free or Die
------- End of Original Message -------

---------
To unsubscribe, send email to <aklug-request@aklug.org>
with 'unsubscribe' in the message body.
Received on Tue Aug 17 17:15:18 2010

This archive was generated by hypermail 2.1.8 : Tue Aug 17 2010 - 17:15:18 AKDT