sss has asked for the wisdom of the Perl Monks concerning the following question:

Hi, i need some help regarding command line execution from perl. I have a .cmd script which sets the property to a key. So i execute this script with two arguments. The first arg is a key in a properties value and the second arg is the varibale which gets the value of the property and gets assigned. Now, i need to call this script from a perl script and gets the value of a property to get assigned to a variable. Using system is not helping. system ("call set_prop.cmd db.server.name dbserver") Please help how can i get this working.
  • Comment on return a variable from command line execution to perl

Replies are listed 'Best First'.
Re: return a variable from command line execution to perl
by Corion (Patriarch) on Aug 15, 2014 at 13:42 UTC

    So, how does the child process communicate the "property" and the "key" to the calling process?

    If that cmd only sets the value in the environment, you will have to call that .cmd in a way very similar (but different to) Get default login environment.

    Maybe the following works as another wrapper .cmd file:

    @echo off cd /d %~dp0 call set_prop.cmd db.server.name dbserver set

    You would then use the output of that intermediate .cmd file as follows from Perl:

    my @output= qx(get_property_values.cmd); chomp @output; print "Got output [$_]" for @output;

    Alternatively, see perlipc.

      Thank you Corion. I figured out a different way. use backticks and then echo the variable at the end. $dbserv=`call set_prop.cmd db.server.name dbserver & echo %dbserver%` This worked fine.

        If you'd like some nicer error handling for the backticks, see capture from IPC::System::Simple.

Re: return a variable from command line execution to perl
by kennethk (Abbot) on Aug 15, 2014 at 14:53 UTC

    I think the easy answer is backticks. Have your child script print the value, and have the parent script invoke with child as my $value = `perl script.pl`;

    For more complex/robust options, follow Corion's advice.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.