Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

.bat and perl mystery

by Anonymous Monk
on Jun 12, 2002 at 16:22 UTC ( #173904=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm perplexed! I have written a perl script that executes a .bat without specifying the absolute path to the file. When it executes in the script it acts as though it has executed but in reality it hasn't. If I execute the .bat file from the command line, it functions accordingly. I guess my question is, is why doesn't it execute from my perl script, and or is their a way to execute a .bat file in a perl script that emulates my command line execution? I have tried executing the .bat file using its absolute path, but without success. I am also using the 'system' command to execute it. I'm not sure if this is the problem, but I have also tried 'exec'and 'open'. Is their a way to gaurantee that my perl script runs in its current directory? Thanks for the advice!

Replies are listed 'Best First'.
Re: .bat and perl mystery
by particle (Vicar) on Jun 12, 2002 at 16:45 UTC

    #!/usr/bin/perl use strict; use warnings; $|++; my $cmd = 'hello world.bat'; my @cmd_args = qw/ testing testing 1 2 3 /; my $exit_code = system( $cmd, @cmd_args ); print 'exit code from ', $cmd, ' is ', $exit_code, $/;
    and 'hello world.bat':
    echo hello world! echo %1 %2 %3 %4 %5
    does this work? system returns the exit code from the command. use backticks or qx// to get the program's STDOUT into a scalar or array.

    ~Particle *accelerates*

      This is it, but I don't think it will be of much help. In order to use "/USEENV", I first have to execute the batch file: 'vcvars32.bat'. If I execute it fromt he command line, things work fine, but when I call it in my script they don't
      sub Build { my $vcvars32 = 'VCVARS32.BAT'; system ($vcvars32); $command = join("", "msdev ", "$workspace", "\\", "$projectname ", + "\/MAKE ", "\"$name ", "\- ", "$activeconfiguration\" ", "\/REBUILD +\/OUT ", "$workspace\\", "$log_name ", "\/USEENV"); $start = localtime time; print "\nProcessing build..."; system "$command"; print "Done\n"; $finish = localtime time; }
        As you've found out, you can't set environment variables in the parent process. When you try, you spawn a new command process, set the environment variable and exit that process, losing whatever changes you made (see perldoc -q environment or here).

        What you can do is use the %ENV hash to set the environment before using VS to build your project.

        I can't remember what vcvars32.bat sets, but something like this:

        $ENV{LIB}='D:\VCDIR\LIB;' . $ENV{LIB}

        should work, as long as it is set before you begin the build.

Re: .bat and perl mystery
by Joost (Canon) on Jun 12, 2002 at 16:35 UTC
    IANAWE (I'm not a windows expert) but it seems to me you need to run a .bat script trough an interpreter. Have you tried sytem("cmd $batchfile") or system("command $batchfile")

    If this doesn't help, can you specify what you mean with When it executes in the script it acts as though it has executed but in reality it hasn't - maybe with a small code example?

    -- Joost downtime n. The period during which a system is error-free and immune from user input.
      Unfortunatley, I can't provide the code, because it's dealing with the 'vcvars32.bat' file (environment for Visual Studio 6.0) I get the message: "Setting environment for using Microsoft Visual C++ tools.", but it hasn't actually changed the environment. If I execute the batch file from the command line and then run my perl script is works fine....DAMN Microsoft!
        execution path:

        run perl script -> start new environment space and run batch file -> return to old environment space inside perl script (no changes made to environment)

        check out tye's SetEnviron.bat for more info on your real problem.

        ~Particle *accelerates*

        Mmmm... Nice problem

        Here's my guess:
        It _does_ adapt your envirnoment, but the valuea are set back once the perl script ends...

        er formait hyarya.
        -- "Life is a house and the next tornado is never far away"
        -- "lovely by nature"

Re: .bat and perl mystery
by insensate (Hermit) on Jun 12, 2002 at 16:41 UTC
    First, I would run a simple file test to make sure your script can see the file, something like

    unless(-e "yourscript.bat") { print "Cannot see yourscript.bat...\n"; }

    Then, a simple system("yourscript"); call should work. Also, if your .bat script writes output, try redirecting it to a file eg. system("yourscript > out.txt");Please post the relevant sections of your code so we can be of more assistance.
      OK, I think this might have something to do with it. It can't see the .bat file, but I'm able to execute it from the command line in the same directory as my perl script.
        Ah...perhaps the .bat file is in a directory in your %PATH% env variable...try hardcoding the entire paths in your script...remember to escape the \ characters for win32 "c:\mydir\myfile" would be "c:\\mydir\\myfile"
Re: .bat and perl mystery
by r0b (Pilgrim) on Jun 12, 2002 at 16:39 UTC
    I'm not quite sure what your problem is here. On my windows 2000 system using the following works as expected: system("myfile.bat");
    Maybe if you posted the code you are using it might shed some light on the matter?

    eval pack "h*", "072796e647022245d445f475454494c5e622b3";

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://173904]
Approved by VSarkiss
LanX thanks Ophelia for the great weather - summer reloaded! :)
[marto]: the wind woke the kids in the early hours of the morning.

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (10)
As of 2017-10-17 12:17 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (230 votes). Check out past polls.