Re: execute vbscript code inline in Perl script
by NetWallah (Canon) on Sep 08, 2017 at 05:17 UTC
|
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.
| [reply] [d/l] [select] |
|
|
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";
| [reply] [d/l] |
|
|
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. | [reply] [d/l] [select] |
|
|
| [reply] |
Re: execute vbscript code inline in Perl script
by vinoth.ree (Monsignor) on Sep 08, 2017 at 03:48 UTC
|
There are three ways to run an external program from perl.
The function qx() returns the STDOUT as a string.
The function system returns the vb script exit code.
The function exec never returns.
In your case use qx() to run your vb script and capture the output in a varible like my $final = qx(vbscript)
All is well. I learn by answering your questions...
| [reply] [d/l] [select] |
|
|
| [reply] |
|
|
my $final = qx("cscript out2.vbs");
print STDOUT "Final: $final\n";
Final: Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
my $final2 = system("cscript out2.vbs");
print STDOUT "Final2: $final2\n";
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
Final2: 56320
Is 56320 the process ID from CScript?
out2.vbs
Dim num1, num2, final, str
num1 = 30
num2 = 50
str = "Final Value: "
final = num1*num2
'MSGBOX str & final
WScript.Quit(final)
'Wscript.Echo final
| [reply] [d/l] [select] |
|
|
Dim num1, num2, final, str
num1 = 30
num2 = 50
str = "Final Value: "
final = num1*num2
'MSGBOX str & final
'WScript.Quit(final)
Wscript.Echo final
my $final = qx("cscript out2.vbs");
print STDOUT "Final: --> $final\n";
Final: --> Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
1500 | [reply] [d/l] [select] |
|
|
|
|
|
Re: execute vbscript code inline in Perl script
by holli (Abbot) on Sep 09, 2017 at 16:37 UTC
|
| [reply] |