Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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.


In reply to Re: Perl/Tk and Javascript by PodMaster
in thread Perl/Tk and Javascript by hackmare

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (4)
    As of 2020-06-03 04:00 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Do you really want to know if there is extraterrestrial life?



      Results (19 votes). Check out past polls.

      Notices?