Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Running local Perl from HTML

by roboticus (Chancellor)
on Mar 11, 2019 at 14:40 UTC ( [id://1231123]=note: print w/replies, xml ) Need Help??


in reply to Running local Perl from HTML

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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-03-29 10:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found