Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^4: Run Perl 5 in the Browser!

by haukex (Archbishop)
on Aug 22, 2018 at 21:24 UTC ( [id://1220872]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Run Perl 5 in the Browser!
in thread Run Perl 5 in the Browser!

So webperl doesnt tra slate to js? I thought it did. Oh well

You're not the first to ask me about that, so I updated http://webperl.zero-g.net to mention that.

WebPerl is a port of the perl binary to WebAssembly, so what is being run really is a perl, plus some glue code between JavaScript and Perl. Your Perl script is parsed and executed like it would normally be when you run perl (since as we know, only perl can parse Perl). The glue code provides a Perl js() function that you give a string of JavaScript code, if that JS code returns an object (hash), array, or function, it is wrapped inside a special Perl object that you can use like a Perl object (with overloaded hash, array, and coderef dereferencing), but all the operations on that object cause the equivalent JavaScript code to be run on the JavaScript object. In that sense, it is kind of like "translating Perl to JS", but in the dynamic, runtime sense, instead of some kind of source-to-source compilation.

So it's perfectly viable to use any JavaScript library of your liking from WebPerl. For example, the included webperl_demo.html shows how to get the jQuery object from JavaScript and use that in Perl, the exact same thing could be done with the Papa object provided by Papa Parse. But then again, since my intention was to replace JavaScript with Perl, personally I'm probably going to stick to Perl modules like Text::CSV_XS for that kind of stuff, and only use JavaScript when I need to do stuff related to DOM manipulation. WebPerl is just meant to be a tool, so you're free to do what you like, of course :-)

A few minor additions.

Replies are listed 'Best First'.
Re^5: Run Perl 5 in the Browser!
by Anonymous Monk on Oct 10, 2018 at 06:36 UTC
    I don't get the purpose of Webassembly here? Is it something like Perl<->Webassembly<->Javascript ? I mean,could it be done without Webassembly?
      could it be done without Webassembly?

      In theory it could, and in fact, my early attempts near the beginning of this year were aimed at compiling to asm.js. asm.js is a strict subset of JavaScript which lends itself towards compilation, and browsers supporting it will be able to do so and execute it much faster (browsers that don't support it can still run it, just slower). However, asm.js requires aligned memory access (Emscripten docs), and the Perl core does quite a bit of that (for example, one can add -Wcast-align to the ccflags in hints/emscripten.sh to see all the warnings). I managed to get 5.26 patched so that it would at least compile and run some basic code, but at the same time ran into other issues, such as that I was trying to use dynamic loading, which in that combination didn't support dynamic memory expansion. WebAssembly, which you can think of as kind of a precompiled asm.js, does not require aligned memory access, and when I switched to static linking and WASM, things were much easier (relatively speaking), and I didn't have to patch Perl (as much).

      So in theory, it would still be possible to do a pure asm.js compilation, if one were to go through the entire Perl core (and any XS modules one wants to add) and change everything that relies on unaligned memory accesses. That's a significant amount of work though. WebAssembly works in any modern browser that supports it, and IMO nowadays requiring a modern browser is no longer a big ask (the days of people being stuck on old IE versions appear to be coming to an end). I also briefly experimented with the WebAssembly "interpreter" that is provided by Binaryen, which would at least allow WebAssembly to be run on older browsers, although probably quite slowly. That might be another option if backwards compatibility with older browsers is required.

      Update:

      I don't get the purpose of Webassembly here? Is it something like Perl<->Webassembly<->Javascript ?

      The perl binary needs to be compiled to either asm.js or WASM, as I explained above. This is because Perl is written in C, and needs to be compiled, "porting" it to JavaScript by hand is basically not an option due to the complexity. Emscripten also does a lot more than that, it provides a virtual environment and standard C libraries that most C programs need to compile and run.

      WebAssembly can be thought of like kind of a precompiled JavaScript - they are different languages but run in the same VM and can call into each other - see e.g. "How does WebAssembly fit into the web platform?". What happens when WebPerl loads is basically that there is JavaScript code that prepares the Perl script and loads it into the virtual filesystem, and then the Emscripten-generated code loads and runs the WebAssembly perl binary. Update 2: The WebAssembly perl binary can then call out to JavaScript using the code in WebPerl.xs (which has been compiled to WASM+JS and linked into the perl binary) and its Perl frontend, WebPerl.pm. I explained the further interaction of Perl and JavaScript a bit more here.

      A few more minor edits.

      I think the idea is that WebAssembly is faster than Javascript, as it's more amenable to a direct translation to machine code.

      But to me, it's not a matter of how well the bear dances, but that the bear dances at all :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-03-28 13:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found