Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Capture output from Win32::Process ?

by banesong (Acolyte)
on Oct 03, 2003 at 18:16 UTC ( [id://296350]=perlquestion: print w/replies, xml ) Need Help??

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

How does Win32::Process display information? I have set the below code to show you what I am doing. My problem is, I need to capture the output of the app and dump it in my current Log file. Any ideas?
Win32::Process::Create( $ProcessObj, $admin_scripts . "\\Process_Table.bat", $admin_scripts . "\\Process_Table.bat -d $db_name -t $table_name", 0, NORMAL_PRIORITY_CLASS|CREATE_DEFAULT_ERROR_MODE, $db_site_dir )|| die ErrorReport(); $ProcessObj->Wait(INFINITE);

janitored by ybiC: Balanced <code> tags surrounding code block, convert mention of Win32::Process to live link to CPAN module

Title edit by tye

Replies are listed 'Best First'.
Re: Capture output from Win32::Process ?
by AcidHawk (Vicar) on Oct 07, 2003 at 05:58 UTC
Re: Win32::Process
by BrowserUk (Patriarch) on Oct 03, 2003 at 19:08 UTC

    Is there some reason you are using Win32::Process instead of system, or qx or backticks?


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
    If I understand your problem, I can solve it! Of course, the same can be said for you.

      Yep. I need to have the process execute in a directory other than that in which the perl commnad is executed (ie you execute my.pl in C:\, but the process always needs to occur in c:\this\directory) If you have another way to do that without changing the directory that the perl is executing in, please suggest.

        By far the easiest way is to chdir to the directory where you want the script to run, qx// to run it and capture the output and then chdir back to where you were when the command is complete.

        You can use Cwd (part of the standard distribution) to find out where you are. Ie. where to go back to.

        The following shows how I would do it. The 2>&1 is only necessary if you need to capture STDERR as well as STDOUT. The output at the bottom shows that dummy Process_Table.bat I used that a) reports is current directory, b) prints some output to STDERR c) echos some "results".

        #! perl -slw use strict; use Cwd; my $db_site_dir = 'c:\test'; my $db_name = 'TESTDB'; my $table_name = 'TESTTABLE'; my $admin_scripts = 'p:\test'; my $cwd = cwd(); chdir $db_site_dir; my @results = qx[ $admin_scripts\\Process_Table.bat -d $db_name -t $table_name 2>& +1 ]; chdir $cwd; print 'Got error $?' if $?; print "Got:$_" for @results; __END__ P:\test>type Process_Table.bat @echo off @cd @perl -e"warn 'This text is output to STDERR'" @echo Processed DBName: %2, Table: %4 successfully P:\test>296350 Got:c:\test Got:This text is output to STDERR at -e line 1. Got:Processed DBName: TESTDB, Table: TESTTABLE successfully

        If you really, really , really need to do this with CreateProcess(), you could do worse than look at the perl source code to see how backticks and qx// are implemented.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://296350]
Approved by ybiC
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-18 17:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found