简体   繁体   中英

Perl: XUL::GUI how to pass variable between javascript and perl

Perl Expert

I am trying to use Web::Gui to make an app for my team, but run into a roadblock where I need to pass variable between perl and javascript.

My code takes input from a text area, does some processing by perl, and then displays the result by refreshing a via javascript. I saw from the XUL::GUI example on CPAN that perl scripts can be called by “perl()” inside javascript. However that example does not show how to pass the variable between perl and javascript.

Could you please point my in right direction?

use Web::Gui;
display
    ( CENTER 
         H3( 'Pathe!' ),
         ( FORM
              ( TEXTAREA id => 'myText', rows => 10, cols => 50, readonly => 1, TEXT => 'disks' )
              ( INPUT type => 'button', value => 'Search', onclick => function q{
                  var text = document.getElementById('myText').innerHTML;
                  perl( "mySub( text )" );
                  document.getElementById('d').innerHTML = "<b>" + text + "<b/>"; } 
              )
        ),
        ( DIV id => 'd' )
    );

sub mySub {
  return "$_[0]35";
}

I solved the problem. It is just the perl q and qq function:

display
    ( CENTER 
        H3( 'Pathe!' ),
        ( FORM
            ( TEXTAREA id => 'myText', rows => 10, cols => 50, readonly => 1, TEXT => 'disks' ),
            ( INPUT type => 'button', value => 'Search', onclick => \&mySub )
        ),
        ( DIV id => 'd' )
    );


sub mySub {
    my $input = ID(myText)->value;
    $input .= "156";
    gui qq{ document.getElementById('d').innerHTML = '<b>' + \'$input\' + '<b/>'; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM