Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

printing true false values

by HarshaHegde (Sexton)
on Dec 30, 2007 at 15:49 UTC ( [id://659617] : perlquestion . print w/replies, xml ) Need Help??

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

Hello Monks,

I have a piece of code which checks if a string is an IP Address or not and prints a 1 if the string is a valid IP and 0 otherwise.

#!/usr/bin/perl use strict; use warnings; main(); exit; ############################################### sub main { my $ip = qq{127.0.0.1}; print "IP Validity Status:". validate_ip_addr($ip),$/; } sub validate_ip_addr { my ($addr_to_check) = shift; return ( $addr_to_check =~ m/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/ + ); }
If the ip is valid, this piece of code prints:
IP Validity Status:1
However, if the IP is invalid, what I see is:
IP Validity Status:
How do I make it print:
IP Validity Status:0
?

TIA,

Replies are listed 'Best First'.
Re: printing true false values
by McDarren (Abbot) on Dec 30, 2007 at 16:06 UTC
    You have an answer to your immediate problem, however....

    Matching a valid IP address is not quite as simple as it might seem at first. For example, your regex would match 999.999.999.999, which is certainly not a valid IP.

    Rather than trying to roll your own, you might want to take a look at Data::Validate::IP - all the tough work has been done for you.

    Cheers,
    Darren :)

      Thanks a lot Darren. This is definitely the best solution.
Re: printing true false values
by jasonk (Parson) on Dec 30, 2007 at 15:51 UTC
    print "IP Validity Status:". ( validate_ip_addr( $ip ) ? 1 : 0 )."\n";

    We're not surrounded, we're in a target-rich environment!
      Thanks Jason and FunkyMonk, for the quick response. I did think of doing something similar: using the if else. However, by the definition of truth values in perl, 0 represents false and anything else represents true. So, if a boolean expression evaluates to true, it will return a 1 and if it evaluates to false, shouldn't it return a 0?
        perlsyn.pod says:
        Truth and Falsehood
        The number 0, the strings ’0’ and ’’, the empty list "()", and "undef" are all false in a boolean context. All other values are true. Negation of a true value by "!" or "not" returns a special false value. When evaluated as a string it is treated as ’’, but as a number, it is treated as 0.
        What values are considered true and false is not the same thing as what an expression evaluates to. Many Perl operators that evaluate to a boolean evaluate to 1 when true and '' (the empty string) when false.
        you could force numerical context
Re: printing true false values
by jwkrahn (Abbot) on Dec 30, 2007 at 16:05 UTC
    use Socket; my $ip = '127.0.0.1'; print 'IP Validity Status:', inet_aton( $ip ) ? 1 : 0, "\n";
Re: printing true false values
by FunkyMonk (Chancellor) on Dec 30, 2007 at 15:52 UTC
    Change your return to
    return ( $addr_to_check =~ m/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/ ) ? 1 : 0;

Re: printing true false values
by ysth (Canon) on Dec 30, 2007 at 23:49 UTC