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

Greetings monks!
This is somewhat RE voodoo, and while I'm fair with sed, and a bit grepish. I don't feel confident enough to subject my understandings to the internet at large -- I'm working with form fields that will be in a web page.
That said;
I have built a couple of functions that return either domain information, or IP information. While I could simply use a select form field, or a radio/checkbox. I was hoping to simply consolidate, and use the single text field, and based on the content, direct it to the proper function.
So my conditionals are:
1.) is a valid domain name
2.) is an IP address
So I can imagine that the easiest, perhaps even the most efficient, might be to simply grep against an IP;
if (!$input) {print $form; }else{ if $input (d.d.d.d) $input=>$ip; }else{ if $input (valid.domain.name) $input=>$domain; }else{ print $form; }
I realize the above is quite simplistic. But I'm afraid I really need some help in this area. What would be good logic to grep patterns correctly to arrive at a efficient conditional?

Thank you very much for all your consideration, and my sincere apologies if this seems a stupid question.

--chris

#!/usr/bin/perl -Tw
use perl::always;
my $perl_version = "5.12.5";
print $perl_version;

Replies are listed 'Best First'.
Re: How to best filter/direct data based on input structure?
by Kenosis (Priest) on Nov 02, 2013 at 03:13 UTC

    Hi Chris.

    This is not a stupid question. Consider using a couple of modules for the validation tasks, viz., Regexp::Common (for IP addresses) and Data::Validate::Domain.

    Given these, you can do the following:

    use strict; use warnings; use Regexp::Common qw/net/; use Data::Validate::Domain qw(is_domain); my $form = 'This is my form stuff.'; while ( my $input = <DATA> ) { chomp $input; my $results = ( $input =~ /$RE{net}{IPv4}/ or is_domain($input) ) +? $input : $form; print "Input: $input; Results: $results\n"; } __DATA__ 66.39.54.27 perlmonks.com 555.666.777.999 192.168.0.1 %^&.#@!.com

    Output:

    Input: 66.39.54.27; Results: 66.39.54.27 Input: ; Results: This is my form stuff. Input: perlmonks.com; Results: perlmonks.com Input: 555.666.777.999; Results: This is my form stuff. Input: 192.168.0.1; Results: 192.168.0.1 Input: %^&.#@!.com; Results: This is my form stuff.

    The validation results are generated using the ternary operator and the modules' offerings: $input is returned if it's a valid IP or domain; anything else returns $form.

    Hope this helps!

      @Kenosis;
      I can't thank you enough Kenosis, for your kind words, and informative reply.
      Your suggested solution is perfect. I'm not usually so dense. But after working, and tackling the hard parts first. My mind sometimes has a hard time sorting out the easy stuff -- it tends to make it really complicated. :/
      Anyway, I can't thank you enough for indulging me. As I know it probably wasn't really hard for most ppl. I just somehow made it that way.
      Thanks again.

      --Chris

      #!/usr/bin/perl -Tw
      use perl::always;
      my $perl_version = "5.12.5";
      print $perl_version;

        You're most welcome, Chris! Am glad it worked for you.

        My mind sometimes has a hard time sorting out the easy stuff -- it tends to make it really complicated.

        Well, then, we have one more thing in common--besides Perl...

Re: How to best filter/direct data based on input structure?
by LanX (Saint) on Nov 02, 2013 at 02:05 UTC

    > So my conditionals are

    > 1.) is a valid domain name

    > 2.) is an IP address

    You did search, didn't you?

    Googling with your exact wording delivered tons of regex solutions.

    So what's wrong with them? =)

    Cheers Rolf

    ( addicted to the Perl Programming Language)