Win32: Output to console

Discussion in 'Perl Misc' started by Dave..., Dec 22, 2003.

  1. Dave...

    Dave... Guest

    I'm using Perl on a windows machine to automate Visual Studio .NET C++
    builds each day. The command I run is the VS commandline program
    devenv.exe which outputs log and error information to the console if
    run directly on the commandline. For some reason I am unable to
    capture or redirect this output to the console when I run the command
    from Perl until the entire build is complete. Using system() I get no
    ouput at all so I tried the following...

    $|=1;
    open(COMPILELOG, "$devenvcommand |");
    while( <COMPILELOG> )
    {
    print;
    }
    close(COMPILELOG);

    This does capture the output, but doesn't print it out until the
    entire build is completed instead of outputing during the build. I
    also tried adding 2>&1 to the command but it did not help.

    Is there a way to output in real time the way it works on the
    commandline or could someone explain to me why it cannot be done?

    Dave...
    Dave..., Dec 22, 2003
    #1
    1. Advertising

  2. On 22 Dec 2003 10:42:54 -0800
    (Dave...) wrote:
    <sniped for brevity>
    > Is there a way to output in real time the way it works on the
    > commandline or could someone explain to me why it cannot be done?


    Look over the IPC::Open2 and IPC::Open3 modules (standard with Perl).
    They *may* give you what you want.

    HTH

    --
    Jim

    Copyright notice: all code written by the author in this post is
    released under the GPL. http://www.gnu.org/licenses/gpl.txt
    for more information.

    a fortune quote ...
    Green light in a.m. for new projects. Red light in P.M. for
    traffic tickets.
    James Willmore, Dec 22, 2003
    #2
    1. Advertising

  3. Dave...

    Ben Morrow Guest

    (Dave...) wrote:
    > I'm using Perl on a windows machine to automate Visual Studio .NET C++
    > builds each day. The command I run is the VS commandline program
    > devenv.exe which outputs log and error information to the console if
    > run directly on the commandline. For some reason I am unable to
    > capture or redirect this output to the console when I run the command
    > from Perl until the entire build is complete. Using system() I get no
    > ouput at all


    Have you closed/reopened STD{IN,OUT,ERR}? All my experience on Win32
    suggests that commands run with system() from a perl process running
    in a console window inherit thir STD handles from the parent, and will
    output to the console if that's where they still point.

    You could try system "devenv ... >CON: <CON: 2>CON:", or you could try
    reopening STD{IN,OUT,ERR} to "CON:" before calling system(). You could
    try uning Win32::process instead.

    If you want to capture the output rather than simply have it go to the
    console, I think you're out of luck. Command-line programs test
    whether their input is a console; if it is, they empty their output
    buffer every line, if it isn't, they buffer in huge chunks for
    efficiency. Under unix you'd use a special type of file called a
    'pty', which is basically a two-way pipe that pretends to be a
    console. I don't think there is any equivalent on win32.

    Ben

    --
    "If a book is worth reading when you are six, *
    it is worth reading when you are sixty." - C.S.Lewis
    Ben Morrow, Dec 22, 2003
    #3
  4. Dave...

    l v Guest

    Dave... wrote:
    > I'm using Perl on a windows machine to automate Visual Studio .NET C++
    > builds each day. The command I run is the VS commandline program
    > devenv.exe which outputs log and error information to the console if
    > run directly on the commandline. For some reason I am unable to
    > capture or redirect this output to the console when I run the command
    > from Perl until the entire build is complete. Using system() I get no
    > ouput at all so I tried the following...
    >
    > $|=1;
    > open(COMPILELOG, "$devenvcommand |");
    > while( <COMPILELOG> )
    > {
    > print;
    > }
    > close(COMPILELOG);
    >
    > This does capture the output, but doesn't print it out until the
    > entire build is completed instead of outputing during the build. I
    > also tried adding 2>&1 to the command but it did not help.
    >
    > Is there a way to output in real time the way it works on the
    > commandline or could someone explain to me why it cannot be done?
    >
    > Dave...



    I do not know where you are in this post, however, here is my $.02

    1. try launching a new process to exec the build command using one of
    several options. Use the command 'cmd', or 'start' or use Win32::process

    The following will not print until dir /s is complete
    perl -e " print `dir /s`"

    The following will print in real time (run cmd /? at a command prompt
    for more info).
    perl -e " print `cmd /c dir /s`"

    start is another *cool* program provided with win 95 and above. I use
    it extensively on a win 2000 server to start and capture the output of
    17 separate oracle backups on 11 unix servers. (run start /? at a
    command prompt for more info).
    for example, your main script could run, either backtics or system(),
    `start cmd /c $devenvcommand`
    or keeping with my example
    perl -e " `start cmd /c dir /s` "

    Please note that you might want to use the /k option vs /c when using
    the cmd command in order to view your build output.


    2. Not sure if you need to capture the output from the build, try
    redirecting the build output to a log file if one is not already
    created. Then, prior to your build command, start another process to
    *tail* the log file, sleep for 2 seconds before opening the log file if
    needed. When the main script is done running the build command, you can
    continue on and parse the build output if required; or a simple print
    "\a\a"; :)

    There are windows ports of the unix tail program on the net for free.
    There is also a perl version of tail
    http://search.cpan.org/~sdague/ppt-0.12/bin/tail
    or
    File::Tail
    or
    I seem to remember a Perl Recipe of the Day regarding a tail program (
    you'll need to search for it ). It involved seek() and resetting the
    EOF, sleeping, then re-reading the file.

    Len
    l v, Dec 24, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Finney
    Replies:
    2
    Views:
    986
    Egor Bolonev
    Jun 30, 2003
  2. Egor Bolonev
    Replies:
    0
    Views:
    475
    Egor Bolonev
    Jun 30, 2003
  3. pratap
    Replies:
    3
    Views:
    454
    James Kanze
    Jul 19, 2007
  4. Replies:
    5
    Views:
    3,426
    James Kanze
    Mar 4, 2008
  5. Ilya Zakharevich

    Console output on Win32?

    Ilya Zakharevich, May 8, 2009, in forum: Perl Misc
    Replies:
    6
    Views:
    343
    RedGrittyBrick
    May 11, 2009
Loading...

Share This Page