Win32::HTA - HTML Applications with Perl as local backend
use Win32::HTA; my $hta = Win32::HTA->new(); # Simple MessageBox with return value my $chosen = $hta->show( TITLE => 'Important Information!', DLG_HTML => qq( <p id="msg_txt">Something happened!</p><br> <button id="bok" type="button" onclick="ret_handler();">OK</button> <button id="bcancel" type="button" onclick="esc_handler();">CANCEL</button> ), CSS => q( p { width: 250px; text-align: center; } button { width: 80px; margin-left: 30px;} ), ON_LOAD => q[ bok.focus(); ], RET_HANDLER => q(pipe_string('OK')), ESC_HANDLER => q(pipe_string('CANCEL')), ); print $chosen, "\n"; # Reset to default $hta->clear(); # AJAX communication require JSON; $hta->show( AJAX => sub { my($request) = @_; print JSON::encode_json($request); my $ok = int(rand()+0.5); return { succeeded => $ok, data => { name => $request->{request}, text => $request->{data}, }, $ok ? () : ( errtxt => 'Something went wrong!'), }; }, IE_MODE => 11, BG_COLOR => '#888800', DLG_HTML => qq( <button id="but" type="button" onclick="bpress()">Talk to Perl</button><br> ), RET_HANDLER => q(but.click();), CSS => 'button {width: 150px;}', JS_HEAD => q( function bpress() { var reply = ajax_request({request : "test", data : 'testdata'}); if ( reply['succeeded'] ) { alert(reply['data']['text']); } else { alert(reply['errtxt']); } close(); } ), );
HTML Applications are an easy way to build simple and not so simple GUI applications using HTML with script languages supported by Internet Explorer (Javascript, VBScript and even PerlScript if you are using ActivePerl).
As I am using Strawberry Perl, I thought launching mshta.exe from inside perl and then communicating via AJAX could be interesting.
Creates a new Win32::HTA object with the options given. See "OPTIONS"
Resets the object to the default values and sets the given values afterwards. For options see "OPTIONS"
Displays the Application window with mshta.exe. Waits for the application to close. Will return the string written by pipe_string() from the javascript side of the HTA unless the 'AJAX' option is used.
For options see "OPTIONS".
There are lower case accessor methods for all options. So instead of passing options to new() clear() and show() you can do e.g.:
$hta->title("Titletext"); $hta->show();
Reference to a subroutine that acts as a request handler. It will get passed a reference to whatever you request from the javascript side of your HTA. It will be already parsed by JSON::decode_json, so you can access the contents directly.
If you want to return something it has to be a reference to an array or hash. It will be run through JSON::encode_json() and parsed by JSON.parse on the javascript side automatically.
You can use the javascript function 'ajax_request(<obj>)' from inside your javascript blocks given in the options JS_HEAD or JS_BODY. The synopsis has a working example.
Shortcut for setting the background color for the HTML <body> tag.
Verbatim style sheet block. It will be set inside the <head> tag.
If set to a true value will output the generated HTML to stderr.
The HTML text for the interface. It will be placed inside a div with id '__DIV__' that is a direct child of <body>
Javascript code that gets called when <Escape> is pressed. The event is bound to the HTML <body>. There is a corresponding javascript funtion called 'esc_handler(obj)' that will be called
Number of pixels to adjust the height of the main window to account for window decorations. Defaults to 44.
additional tags place at the beginning of the header (before JS_HEAD)
Internet Explorer version that will be emulated by mshta.exe. Defaults to 8. This could be important especially for javascript capabilities.
The tcp/ip port that will be used for listening to ajax requests. The default is 0, which will let the OS chose the port. This is the safe option. Use at your own risk.
Javascript code that gets inserted after the main <div> tag.
Javascript code that gets inserted inside the <head> tag.
Javascript code that gets inserted inside a window.onload handler.
Reference to a subroutine that will be called with expanded HTML text as only parameter after variable expansion. Has to return the HTML text. Use at your own risk.
Reference to a subroutine that will be called with the HTML template text as only parameter before variable expansion. Has to return the new template text. Use at your own risk.
Javascript code that gets called when <Return> is pressed. The event is bound to the HTML <body>.
Text for the <title> tag.
Number of pixels to adjust the width of the main window to account for window decorations. Defaults to 24.
There are some predefined utility functions you can use in your javascript code:
Use this for dumping object information if your choosen IE Version does not contain the better JSON.stringify(obj).
Wrapper for obj.addEventListener() or obj.attachEvent() that should do the right thing for the different IE versions.
Writes the string to stdout and closes the HTA. Will only work when you don't use AJAX for communication.
Sends the stringified object data as AJAX request to the perl scripts callback that has been specified with the AJAX option.
Thomas Kratz <tomk@cpan.org>
Copyright (C) 2016 by Thomas Kratz
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.
To install Win32::HTA, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Win32::HTA
CPAN shell
perl -MCPAN -e shell install Win32::HTA
For more information on module installation, please visit the detailed CPAN module installation guide.