Webperl requires at the moment to statically bundle all needed modules.
The following Proof of Concept shows how to dynamically use non-bundled modules.
The modules need to be pure Perl and have to be present inside the $WEBLIB directory on your server (respecting the same domain policy avoids much trouble)
I just copied the desired libs from my installation and listed allowed modules to %INC_FETCH (to limit unnecessary traffic) .
The following page demonstrate how to use Data::Dump from weblib.
Data::Dump is currently not bundled with Web-Perl. (Data::Dumper is since it's core)
The mechanism of adding a call-back to @INC to dynamically fetch modules from various sources is described in require
<!doctype html> <html lang="en-us"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>WebPerl <script> Demos</title> <script type="text/javascript"> function xfetch(url) { var req = new XMLHttpRequest(); req.open('GET', url , false); req.send(null); if(req.status == 200) { return req.responseText; } else { return ""; } } // alert(xfetch('http://localhost:5000/lib/Data/Dump.pm')); // alert(xfetch('http://localhost:5000/webperl.js')); </script> <script src="webperl.js"></script> <!-- Please see the documentation at http://webperl.zero-g.net/using.h +tml --> <script type="text/perl"> use warnings; use strict; use Data::Dumper; use WebPerl qw/js/; BEGIN { my $WEBLIB = 'localhost:5000/lib'; # gather source of module my $fetch = sub { my ($module_path) = @_; return js("window.xfetch('http://$WEBLIB/$module_path')"); }; # allowed modules in weblib my %INC_FETCH = ( 'Data/Dump.pm' => 1, ); # loader hook for @INC my $loader = sub { my ($sub,$filename) = @_; if ( $INC_FETCH{$filename}) { my $source = $fetch->($filename); unless ($source) { warn "Fetching $filename from $WEBLIB failed"; return; } open my $fh_source, "<", \$source; my $pre = ''; #$pre = qq{warn '*** Loading $filename ***';}; return (\$pre, $fh_source); } return; }; push @INC, $loader; } use Data::Dump qw/pp/; my $HoA = { map { $_ => [reverse 1..5] } "a".."d" }; warn pp $HoA; #use Data::Dumper; #warn Dumper $HoA; </script> <!-- Optional STDOUT/STDERR text area (if you don't use this, output g +oes to Javascript console) --> <script> window.addEventListener("load", function () { document.getElementById('output') .appendChild( Perl.makeOutputTextarea() ); }); </script> </head> <body> <p>This is a demo of <a href="http://webperl.zero-g.net" target="_blan +k">WebPerl</a>!</p> <div id="output"></div> <div id="buttons"> <button id="my_button">Testing!</button> </div> </body> </html>
OUTPUT:
{ a => [5, 4, 3, 2, 1], b => [5, 4, 3, 2, 1], c => [5, 4, 3, 2, 1], d => [5, 4, 3, 2, 1], } at /tmp/scripts.pl line 56.
DISCLAIMER: This code is beta and follows the release often paradigm.
Successfully tested with Chrome AND Firefox. FF showed new "Content Security Policy" problems, I didn't have the time to dig into and install the necessary PLACK CORS modules. °
The principle is universal, as soon as webperl can run in a browser and has the capacity to dynamically fetch code, using unbundled (pure) Perl modules becomes trivial.
Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery
FootballPerl is like chess, only without the dice
°) works like a charm in FF , forgot to disable "noscript" filtering for localhost! ;)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: [WEBPERL] dynamically importing non-bundled modules via http
by haukex (Archbishop) on Nov 10, 2018 at 15:05 UTC | |
by LanX (Saint) on Nov 10, 2018 at 17:02 UTC | |
Re: [WEBPERL] dynamically importing non-bundled modules via http
by haukex (Archbishop) on Mar 10, 2019 at 20:40 UTC | |
Re: [WEBPERL] dynamically importing non-bundled modules via http
by LanX (Saint) on Nov 09, 2018 at 17:26 UTC | |
Re: [WEBPERL] dynamically importing non-bundled modules via http
by haukex (Archbishop) on Jan 22, 2019 at 17:56 UTC |