slick.user has asked for the wisdom of the Perl Monks concerning the following question:

Can I execute VB script inline in Perl script and get the "final" value from VB to set to Perl variable?

Below is just simple code from VB script.

Dim num1, num2, final num1 = 30 num2 = 50 final = num1*num2

Replies are listed 'Best First'.
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.

      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.

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...
      There are three ways to run an external program from perl.

      There aren't just those three built in to Perl, e.g. there are piped opens, but even so I recommend a module instead.

      I didn't get any value when I execute the command below from Perl.

      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

        If I echo in vbs, this value didn't store into Perl.

        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

Re: execute vbscript code inline in Perl script
by holli (Abbot) on Sep 09, 2017 at 16:37 UTC
    You can execute VBScript code kindof inline if you use ActivePerl and the thing they call PerlScript, which allows you write code for the Windows Scripting Host (WSH) in Perl. WSH code files can have different languages in them. See here as an example how to intermix Perl and VBS.


    holli

    You can lead your users to water, but alas, you cannot drown them.