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

I've been tasked to convert some Perl code that previously ran in a linux environment to ActivePerl in a Windows environment. I am very new to Perl. My issue is a previous commmand returning multiple lines to a variable was similar to: my @progname_output = `progname l`; Now this command when run from a windows cmd needs to run after a batch file is run to set some environment variables. So, in a cmd prompt I would need to run >set_env_hab.bat >progname l to get my output. Any advice on how to run the .bat file so that my variable gets the returned data of the program? Thanks!

Replies are listed 'Best First'.
Re: Converting to ActivePerl
by GrandFather (Saint) on Oct 26, 2011 at 01:50 UTC

    Is it sensible to move the contents of the batch file into Perl? You can use the Perl variable %ENV to manipulate the environment that will be seen by a program run using backticks.

    True laziness is hard work
      Thanks.
Re: Converting to ActivePerl
by syphilis (Archbishop) on Oct 26, 2011 at 02:27 UTC
    The perl program is capable of setting environment variables - eg:
    $ENV{FOO} = 'foo';
    but, for your script to be able to do that, you need to know in advance the names of these env vars, and the values they need to take on.

    If that's not the case, then things get a bit more complicated.
    I would probably open the batch file for reading and parse its contents for the settings that need to be made:
    use strict; use warnings; open RD, '<', 'foo.bat' or die $!; while(<RD>) { if($_ =~ /set\s/i) { my @v = split /=/, $_; my $n = (split /\s/, $v[0])[-1]; $ENV{$n} = $v[1]; print "\$ENV{$n} : $ENV{$n}\n"; } } my @progname_output = `progname l`;
    The regexes might need some adapting, depending upon what the contents of the batch file can be.

    Cheers,
    Rob
      Thank you.
Re: Converting to ActivePerl
by BrowserUk (Patriarch) on Oct 26, 2011 at 03:15 UTC
    I've been tasked to convert some Perl code that previously ran in a linux environment to ActivePerl in a Windows environment. I am very new to Perl.

    Ask your boss for a training course in Perl.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Here's one who the music of his own vain tongue doth ravish like enchanting harmony.

        If you were an intern and told you should perform an appendectomy, would you expect to have to look up how on the internet?

        Programming is more complex than most surgery.

        Accepting tasks for which you are not equipped is foolish. Being allocated tasks for which you are not equipped is bordering on criminal.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.