#!/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 { // ://?# // 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 match // 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("
");
        writeln("" + testURI + "");
        writeln();
        writeln(ReURI.compile(ReURI)); // this'll print a stringified-hash in perl, stupid
        writeln();
        writeln("   Scheme: " + testURI.replace( Scheme, "$1" ) );
        writeln("Authority: " + testURI.replace( Authority, "$2" ) );
        writeln("     Path: " + testURI.replace( Path, "$3" ) );
        writeln("    Query: " + testURI.replace( Query, "$1" ) );
        writeln(" Fragment: " + 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 + ")" +  Ko[0] + "=" + Ko[1] );
        }
        
        writeln();
    
        for(var ix = 0; ix < test.length; ix++) {
            writeln(ix + " : " + test[ix] );
        }
    
        write("
"); } catch (e) { writeln("

" + e + "

"); } } //' URLMAGIC('http://a.b.c/d/f?a=b;c=%5B%20%21%20%23%20%25%20%25%5D#hello world!'); ]); __END__