Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Chuma:

As has been mentioned, it's not a big deal to have a local server running. As an example, you could use HTTP::Server::Simple::CGI or other server package to build a simple perl server that will serve up your web page and let you interact with it. As a trivial example, this is a simple program that will serve up a web page and accept data:

#!env perl { package PythonServer; use HTTP::Server::Simple::CGI; use base qw(HTTP::Server::Simple::CGI); use Data::Dump 'pp'; # URL paths to monitor my %dispatch = ( '/fetch' => \&fetch_page, '/save' => \&save_data, ); # Look up the path and execute the associated routine, or # give a typical 404 error sub handle_request { my ($self, $cgi) = @_; my $path = $cgi->path_info(); my $handler = $dispatch{$path}; if ("CODE" eq ref $handler) { print "HTTP/1.0 200 OK\r\n"; $handler->($cgi); return; } print "HTTP/1.0 404 Not found\r\n", $cgi->header, $cgi->start_html("Cheese Shoppe"), $cgi->h1("Sorry, We're fresh out of that one."), $cgi->end_html; } sub fetch_page { my $cgi = shift; return if !ref $cgi; print STDERR "Fetched page!\n"; open my $FH, '<', 'pm_1231092.html' or die "Can't open file: $ +!\n"; local $/; my $t = <$FH>; print $cgi->header; print $t; } sub save_data { my $cgi = shift; return if !ref $cgi; print STDERR "Got data: ", pp($cgi->{param}), "\n"; } } # Start up the server use strict; use warnings; my $pid = PythonServer->new(8088)->run; print "PID = $pid, press ^C to stop\n"; my $t = <>;

And here's the web page that it will serve up:

<html> <head> <title>A whimsical form</title> </head> <body> <h2>What is the airspeed of a laden swallow?</h2> <form action="/save"> <table class="things"> <tr><th>Number of Cocoanuts:</th> <td><input type="text" name="NumCocoanuts"/></td></tr> <tr><th>Swallow Type:</th> <td><select name="SwallowType"> <option>African</option> <option>European</option> </select> </td> </tr> </table> <input type="submit" value='Calculate Airspeed'/> </form> </body> </html>

This is an admittedly crude example, but it works. All you need to do is add subroutines to the %dispatch table to let the server handle other actions, create the appropriate web pages, and write the JavaScript you want to use to handle the user interactions.

The console output of a sample run gave me:

$ perl pm_1231092.pl PythonServer: You can connect to your server at http://localhost:8088/ Fetched page! Got data: { NumCocoanuts => [1], SwallowType => ["European"] }

I hope this gives you a starting point to play with.

Note: Internet Explorer for the win! That's a phrase I never thought I'd utter. Frikkin' Chrome kept blocking my attempt at putting in the html page of the example in there.

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re: Running local Perl from HTML by roboticus
in thread Running local Perl from HTML by Chuma

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-03-28 16:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found