Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

All attempts to match data from input fail. Please help.

by taint (Chaplain)
on Dec 07, 2013 at 01:24 UTC ( [id://1066091]=perlquestion: print w/replies, xml ) Need Help??

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

Greetings, Monks.

I've been struggling with this problem for far too long. I seem to get real close. But for some reason, I'm missing a piece of the puzzle, I think. This is probably (should be) simple. :(

I'm attempting to provide 1 text input form field in a web page. The input has 2 choices; IP address, or Domain name. The problem I have is creating a working/effective matching scheme. I think I have all the bits. But I can't seem to focus wide enough to see the whole picture, and put the bits together. Here are the bits/logic I'm attempting to use

#!/usr/bin/perl -w use strict; # seems like an RE might be the easiest use Data::Validate::IP qw(is_ipv4); use Data::Validate::Domain qw(is_domain); # the forms text input name is,not surprisingly, input my $ip = param("input")||""; my $dom = param("input")||""; print qq( <form method="post" action="tester.cgi"> <label for="input">Input: </label><input type="text" name="input" valu +e="" /> <input type="submit" value="Post Input" /> </form>); # how do I know, how can I match? if (is_ipv4($ip)) { print "Looks like an ipv4 address"; }elsif (is_domain($dom)) { print "Looks like a domain\n"; }else{print "We got nadda";}
Yea, I know. Pretty much worthless. But even complete examples were worthless -- they didn't work. :(

Apologies for the mess. But, at this point, that's what my head is.

--Chris

Hey. I'm not completely useless. I can be used as a bad example.

Replies are listed 'Best First'.
Re: All attempts to match data from input fail. Please help.
by Your Mother (Archbishop) on Dec 07, 2013 at 03:04 UTC

    And because reasons… and because <readmore/> is so hard to spell…

    File 1066091.cgi

    #!/usr/bin/env perl use strictures; use CGI qw(:standard); use Data::Validate::IP "is_ipv4", "is_ipv6"; use Data::Validate::Domain "is_domain"; use JSON; if ( my $thing = param("ip_or_domain") ) { print header("application/json"), to_json({ ipv4 => is_ipv4($thing) ? \1 : \0, ipv6 => is_ipv6($thing) ? \1 : \0, domain => is_domain($thing) ? \1 : \0 }); } else { print header("text/html"), start_html(-title => "OHAI", -script => { -type => "text/javascript", -src => "//ajax.googleapis.com/ajax/li +bs/jquery/1.10.2/jquery.min.js" }), h1("MUCH IP. SO OCTET. WOW."), start_form(), p({-style => "width:48%; display:inline-block; text-align:righ +t", -class => "label"}, "IP or Domain"), p({-style => "padding-left:1%;width:50%; display:inline-block" +}, textfield(-name => "ip_or_domain", -style => "width:17em", -placeholder => "Enter an IP or Domain to verify +")), p({-style => "text-align:center", -class => "results"}), end_form(), <<' __jQuery', # The leading four spaces have to match at e +nd. <script type="text/javascript">jQuery(function($){ var $in = $("input[name=ip_or_domain]"); var $form = $in.closest("form"); $form.submit(function(e){ e.preventDefault() }); $in.keyup(function(e){ $(".results").children().remove(); $.ajax({ dataType: "json" ,url: "/" ,data: { ip_or_domain:$in.val() } ,success: function(data){ for ( var key in data ) { if ( data.hasOwnProperty(key) && data[key] ) { var $i = $("<div style='color:#900'></div +>"); $i.hide(); $(".results").append($i); $i.text("Looks like " + key).fadeIn(); } } if ( ! $(".results").children().length ) $(".results").hide().append("<i>nothing</i>") +.fadeIn(); } }); }); })</script> __jQuery end_html() ; } exit 0;

    Update: moved the end_html().

    Run it and visit it, probably at http://localhost:8080…

    plackup -l :8080 -L Shotgun -MPlack::App::WrapCGI \ -e 'Plack::App::WrapCGI->new( script => "1066091.cgi" )'

    Don’t write code like this so close to Christmas. You will make the baby Jesus cry.

      WOW. I'm overwhelmed, Your Mother.

      Nice complete, piece of work!

      It gives me goose bumps, all over.

      ^%#^%# Now I'll have to find something at least as nice, to give you, in return. For Christmas.

      Seriously. Thanks. :)

      --Chris

      Hey. I'm not completely useless. I can be used as a bad example.
      
Re: All attempts to match data from input fail. Please help. [SOLVED]
by taint (Chaplain) on Dec 07, 2013 at 01:38 UTC
    Greetings, Monks.

    I guess I just needed to ask here to get the answer! Shortly after posting this message. It came to me. I needed to make a small adjustment to the use CGI; line

    # original version use CGI;
    to
    # new version use CGI qw/:standard/;
    and problem solved!

    As always, best wishes.

    --Chris

    Hey. I'm not completely useless. I can be used as a bad example.
    
      Very glad you found the solution; congratulations.

      For more info on how that works, search this site for 'teddy bear'.

      Also note that simply using your local text editor to write out the question (complete with code example and PM-approved tags) will often produce the 'teddy bear effect,' without any need to actually post it.

      That course will excuse you from having to apologize in advance: (" I seem to get real close. But for some reason, I'm missing a piece of the puzzle, I think. This is probably (should be) simple.")

      If I've misconstrued your question or the logic needed to answer it, I offer my apologies to all those electrons which were inconvenienced by the creation of this post.
        Sorry, ww.

        It was such a stupid simple problem. I felt the need to apologize, in advance.

        But next time.. forget it! No excuses, no apologies. Got it? :)

        --Chris

        UPDATE If you'd like to learn more. I left a note at the bottom of my personal page, that pretty well sums it up. taint :)

        Hey. I'm not completely useless. I can be used as a bad example.
        

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-03-28 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found