Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

[WEBPERL] dynamically importing non-bundled modules via http

by LanX (Saint)
on Nov 09, 2018 at 15:07 UTC ( [id://1225490]=CUFP: print w/replies, xml ) Need Help??

(in continuation to webperl: fetching data over the web)

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 &lt;script&gt; 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

    This is great, thank you very much!

    Just a note, no JavaScript is required, this should also work the same with the code translated to Perl like I showed here :-)

      > with the code translated to Perl like I showed here :-)

      I wanted to have a mostly pure JS solution to eliminate possible other problems in your object wrapper.

      And XHR is not the only possibility to fetch data, FF has even a fetch function, and emscript offers a wget method.

      Having a browser compatibility switch inside JS to fix incompatibilities is the approach I'm used to, i.e. xfetch could adapt to various contexts.

      I tried to design my code as flexible as possible, feel free to patch functions according to your needs. :)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re: [WEBPERL] dynamically importing non-bundled modules via http
by LanX (Saint) on Nov 09, 2018 at 17:26 UTC
    Should have added that this opens the path to interactive playgrounds for Perl programming, as long as it doesn't include XS.

    This means we could add a test page here to try out Perl code without the need of pre bundling all modules, hence keeping webperl small.

    Concerning XS, I'm no expert in WASM and C, if it's possible to have more than one instance running one could probably do the dynamic linking via a JS bridge. ...

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re: [WEBPERL] dynamically importing non-bundled modules via http
by haukex (Archbishop) on Mar 10, 2019 at 20:40 UTC

    I've just updated the WebPerl repo with use_http.html, which implements this idea. Since I've been thinking about how to get WebPerl into Web Workers, and synchronous HTTP requests are not deprecated there, this idea might become useful there!

Re: [WEBPERL] dynamically importing non-bundled modules via http
by haukex (Archbishop) on Jan 22, 2019 at 17:56 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://1225490]
Approved by hippo
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-25 12:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found