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

From: Arthur Corliss <acorliss@nevaeh-linux.org>
Date: Tue Aug 17 2010 - 20:27:37 AKDT

On Tue, 17 Aug 2010, Lee wrote:

> 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

Perl actually makes this kind of routine very simple, but you should think
1 == true, 0 == false:

   sub isValidIP ($) {
     my $input = shift;

     return $input =~ m#^(\d+)(\.\d+){3}$#sm;
   }

The return value of a regex match is a boolean value. BTW, you need to make
sure anchors are used in your regex. Otherwise the subroutine should be
more correctly called "looksToContainAnIP".

> 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.

Your computer has no concept of true/false, but it does have the concept of
1/0 logical results based on flags and registers. Perl doesn't see the need
to teach your computer those concepts, it's pure redundant syntactic sugar.
Even in some languages where those constants exist integral values are
usually supported in all boolean operations.

         --Arthur Corliss
           Live Free or Die
---------
To unsubscribe, send email to <aklug-request@aklug.org>
with 'unsubscribe' in the message body.
Received on Tue Aug 17 20:27:50 2010

This archive was generated by hypermail 2.1.8 : Tue Aug 17 2010 - 20:27:50 AKDT