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.


In reply to Re: All attempts to match data from input fail. Please help. by Your Mother
in thread All attempts to match data from input fail. Please help. by taint

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.