Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Perl/Tk and Javascript

by PodMaster (Abbot)
on Aug 05, 2002 at 09:43 UTC ( #187613=note: print w/replies, xml ) Need Help??


in reply to Perl/Tk and Javascript

Yes, but why don't you just use a regular browser to interpret the javascript chunk? Win32::OLE may be involved ;)

1. Sure, but have you checked CPAN?

2. Impossible. Well, not really. You'll be able to "simulate" JavaScript in Perl, but would you want to? A lot of the stuff would be relatively easy to do, but what dom will you choose and all that other stuff?

3. Dunno about rhino, but you'd XS it.

4. dunno how NJS works, but if you're going to be using whatever JS interpreter, write an XS interface to it.

After I wrote what I have so far, I clicked on my link above, and I came up with JavaScript.pm - Execute JavaScript from within Perl

JavaScript.pm is an interface to the SpiderMonkey JS engine. It lets you execute JS code, call JS functions, bind Perl classes to JS, import Perl functions to JS, precompile and exeute scripts among many other things. It does conversion between Perl and JS datatypes.

So just use that, or do what that guy did. You still of course have to write a subclass of Tk::HTML if that's how you're intending to use it.

update: Seeing as apparently you have not checked cpan, or ran accross JavaScript, I suggest you take a gander at How to RTFM. Because I practically worship that guide, I always check CPAN first, and so should you.

update: Also, I checked al the ppm repositories available to me, and have not found JavaScript on them, so i'm going to build a ppm of it, and slap it up on my ppm repository (win32 only).

update: I'm having some problems building JavaScript, so it might take a while (damn, I can't do anything in under 5 minutes, rrr ;)

The problems I have are the same as described here

update: I have resolved the errors, and put the PPM up there. Enjoy (if u can ;).

update: Since I bothered to compile this beast, I had to plug some of my javascript into it, check this out:

#!/usr/bin/perl use strict; use JavaScript; my $runtime = new JavaScript::Runtime; my $context = $runtime->create_context(); $context->bind_function( name => 'write', func => sub { print @_ } ); # the pod example, only changed q!! to q[] my $rval = $context->eval(q[ function strange(out, times) { times = Math.abs(times); for(i = 0; i < times; i++) { write(out); } } strange("JavaScript.pm rox!", 10); undefined; ]); ## and now for some of myyyy tests, like rebular expressions ;! $context->bind_function( name => 'writeln', func => sub { print "$_\n" for @_ }, ); $rval = $context->eval(q[ function URLMAGIC(URL) { var testURI = new String(URL); try { // <SCHEME>://<AUTHORITY></PATH>?<QUERY>#<FRAGMENT> // since JavaScript RegExp don't support (?:), i gotta (()) var ReURI = new RegExp( '^' + // beginning of line '([^:/?#]+)?' + // ':' + // for every \ you want to be literal, + '(//([^/?#]*))?' + // make it literal by e\\scaping '([^?#]*)' + // path '(\\?([^#]*))?' + // query '(#(.*))?' + // '$' , // end of line (no real point in having + this 'i' ); // or I just use these along with "String".replace instead of matc +h // Mozilla couldn't handle the /^([^:/?#]+):.*/; var Scheme = new RegExp("^([^:/?#]+):.*"); +// , "$1" var Authority = new RegExp("^([^:/?#]+)?:\/\/([^\/?#]*).*"); +// , "$2" var Path = new RegExp("^([^:/?#]+)?:(\/\/[^\/?#]*)?([^?#]*).* +"); // , "$3" var Query = /^[^?]*\?([^#]*).*/; // , "$1" var Fragment = /[^#]*#(.*)$/; // , "$1" var test = new Array(); test = testURI.match(ReURI); writeln("<pre>"); writeln("<a href='" + testURI + "'>" + testURI + "</a>"); writeln(); writeln(ReURI.compile(ReURI)); // this'll print a stringified- +hash in perl, stupid writeln(); writeln("<b> Scheme:</b> " + testURI.replace( Scheme, "$1" ) + ); writeln("<b>Authority:</b> " + testURI.replace( Authority, "$2 +" ) ); writeln("<b> Path:</b> " + testURI.replace( Path, "$3" ) ) +; writeln("<b> Query:</b> " + testURI.replace( Query, "$1" ) +); writeln("<b> Fragment:</b> " + testURI.replace( Fragment, "$1" + ) ); writeln(); var LeQuery = testURI.replace( Query, "$1" ); LeQuery = unescape( LeQuery.replace(/\+/g,' ') ); //unescape % +HEX writeln(LeQuery); LeQuery = LeQuery.split(/[;&]+/); for(var ix = 0; ix < LeQuery.length; ix++ ) { var Ko = new Array(2); Ko = LeQuery[ix].split('='); writeln(ix + ")<b>" + Ko[0] + "</b>=" + Ko[1] ); } writeln(); for(var ix = 0; ix < test.length; ix++) { writeln(ix + " : " + test[ix] ); } write("</pre>"); } catch (e) { writeln("<h1>" + e + "</h1>"); } } //' URLMAGIC('http://a.b.c/d/f?a=b;c=%5B%20%21%20%23%20%25%20%25%5D#hello +world!'); ]); __END__
JavaScript.pm rox! JavaScript.pm rox! JavaScript.pm rox! JavaScript.pm rox! JavaScript.pm rox! JavaScript.pm rox! JavaScript.pm rox! JavaScript.pm rox! JavaScript.pm rox! JavaScript.pm rox!

http://a.b.c/d/f?a=b;c=%5B%20%21%20%23%20%25%20%25%5D#hello world!

HASH(0x1ab5180)

   Scheme: http

Authority: a.b.c

     Path: /d/f

    Query: a=b;c=%5B%20%21%20%23%20%25%20%25%5D

 Fragment: hello world!

a=b;c=[ ! # % %]

0)a=b

1)c=[ ! # % %]

0 : http://a.b.c/d/f?a=b;c=%5B%20%21%20%23%20%25%20%25%5D#hello world!

1 : http

2 : //a.b.c

3 : a.b.c

4 : /d/f

5 : ?a=b;c=%5B%20%21%20%23%20%25%20%25%5D

6 : a=b;c=%5B%20%21%20%23%20%25%20%25%5D

7 : #hello world!

8 : hello world!

____________________________________________________
** The Third rule of perl club is a statement of fact: pod is sexy.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2020-04-10 04:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The most amusing oxymoron is:
















    Results (49 votes). Check out past polls.

    Notices?