Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: execute vbscript code inline in Perl script

by NetWallah (Canon)
on Sep 08, 2017 at 05:17 UTC ( [id://1198900]=note: print w/replies, xml ) Need Help??


in reply to execute vbscript code inline in Perl script

In addition to the options Vinoth mentions, you can use "open", and various "Capture" modules.

The simplest is probably the "qx" function, AKA backticks.

The important thing is to figure out how to get data out of the VB script.

One option is to use the exit code:

WScript.Quit(returnValue)
Another is to print data to STDOUT:
Wscript.Echo "Return-Value"
In the perl script, you would get the information as $?<<8 (For exit code) or the return value of "qx".

THere are sever other, more complicated options as well .. pipes, sockets, and other IPC mechanisms.

                All power corrupts, but we need electricity.

Replies are listed 'Best First'.
Re^2: execute vbscript code inline in Perl script
by slick.user (Acolyte) on Sep 08, 2017 at 15:32 UTC

    Thanks all for the input. How can I tell my Perl to run VB Script via inline directly command?

    my $vb_cmd = q( Dim num1, num2, final, str num1 = 30 num2 = 50 str = "Final Value: " final = num1*num2 'MSGBOX str & final WScript.Quit(final) ); my $final = qx($vb_cmd); print STDOUT "$final\n";

      To pick up on what NetWallah wrote, personally I usually recommend using a module instead of Perl's builtins like qx// because modules will give you more features and better error handling. In this case, I might suggest IPC::System::Simple's systemx and capturex (the only downside in this case is that STDERR can't be captured, but it doesn't look to me like you need that, although I could be wrong). If you are worried about where the script files might be written, or that they might get overwritten, you could use temporary files (which will have unique names) via the core module File::Temp, like this:

      use warnings; use strict; use File::Temp qw/tempfile/; use IPC::System::Simple qw/capturex/; my ($tfh,$tfn) = tempfile( SUFFIX=>'.vbs', UNLINK=>1 ); print $tfh <<'END_VBS'; Dim num1, num2, final num1 = 30 num2 = 50 final = num1*num2 WScript.Echo(final) END_VBS close $tfh; print "running $tfn\n"; # Debug my $final = capturex('cscript','//nologo',$tfn); chomp($final); print "final: '$final'\n";

      (not fully tested because I'm not on Windows at the moment)

      To have the scripts created in a specific directory, you can use tempfile( ..., DIR=>"C:\\Some\\Path" ). The UNLINK option causes the temporary file to be deleted when the script ends.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-24 08:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found