Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: How to best filter/direct data based on input structure?

by Kenosis (Priest)
on Nov 02, 2013 at 03:13 UTC ( [id://1060877]=note: print w/replies, xml ) Need Help??


in reply to How to best filter/direct data based on input structure?

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!

Replies are listed 'Best First'.
Re^2: How to best filter/direct data based on input structure?
by taint (Chaplain) on Nov 02, 2013 at 03:49 UTC
    @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...

        LOL! That's refreshing to hear. I'm glad to know I'm not the only one. :)

        --Chris

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1060877]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-26 07:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found