Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: Run Perl 5 in the Browser!

by haukex (Archbishop)
on Oct 06, 2018 at 10:33 UTC ( [id://1223619]=note: print w/replies, xml ) Need Help??


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

Are you exposing an API or you have a DSL?

In Perl scripts running inside WebPerl, I expose all of JavaScript to Perl, in two ways:

  1. The js() function from the WebPerl module, which takes an arbitrary string of JavaScript code to execute, and
  2. the return values of the the js() function:
    • strings, numbers, and booleans are copied from JS to Perl, but
    • when the JavaScript code returns an object, array, or function, then as LanX said, WebPerl will return a proxy object of the class WebPerl::JSObject, which overloads array, hash, and code dereferencing, as well as does method autoloading. So when you do any of those operations on those Perl objects, WebPerl will internally build a string of JavaScript code that accesses the original JavaScript object and run that via js(). When it does this, WebPerl copies any assigned values and arguments to functions or methods from Perl to JavaScript, and also wraps subs inside JS functions that, when called, call back into Perl to run the sub (including arguments).

So this way, you can do everything in Perl, and use normal Perl operations to manipulate Perl objects, which actually runs JavaScript in the background.

The interface is documented here. The gory guts can be found in WebPerl.pm and WebPerl.xs (with some supporting code, such as Perl.glue() and Perl.dispatch(), in webperl.js). (You'll see that currently, a lot is done with copying strings back and forth between JS and Perl, and with both JS's and Perl's eval, which in some places could probably be optimized in a future version.)

How can you a access the Dom with Perl?

Since you can use all of JavaScript, you can use native JS (like the example LanX showed), or any JavaScript library of your choice, such as jQuery. I show examples of both in the source webperl_demo.html (don't mind the currently screwy syntax highlighting on GitHub that's highlighting stuff in dark red). Let me know if you have any further questions!

Minor edits to add a few small details.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2025-07-12 03:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.