Professional Web Applications Themes

open CONSOLE? - PERL Beginners

Hi, Is there a way to explicitly open a handle to the "console", or wherever STDOUT/STDERR are directed to by default? My process is running in an environment in which STDOUT and STDERR have already been redirected. I want to intercept these streams when running command line tools, then restore them afterwards. I want to be able to call my command line tool like: my ${ret} = `command 2>&1`; and have ${ret} get the both STDOUT and STDERR. This piece gets called a lot so I want to avoid temp files if I can. This has to work on Windows ...

  1. #1

    Default open CONSOLE?

    Hi,

    Is there a way to explicitly open a handle to the "console", or wherever
    STDOUT/STDERR are directed to by default?

    My process is running in an environment in which STDOUT and STDERR have
    already been redirected. I want to intercept these streams when running
    command line tools, then restore them afterwards. I want to be able to call
    my command line tool like:

    my ${ret} = `command 2>&1`;

    and have ${ret} get the both STDOUT and STDERR. This piece gets called a lot
    so I want to avoid temp files if I can. This has to work on Windows if that
    makes any difference (I think that means no /dev/console, etc.). I know
    about:

    open( RESTORESTDOUT, '>>&STDOUT' );

    and I think I can restore the redirected STDOUT from there, but how to I open
    STDOUT so that it goes to the "console", as if it had never been redirected?

    Hopefully this makes some sense.

    TIA,

    -John
    Perl.Org Guest

  2. #2

    Default Re: open CONSOLE?

    perl.org wrote: 

    open( OUT, 'con' ); # IIRC

    I /think/ you can also use POSIX::fdopen() or IO::Handle::new_from_fd()
    to get the standard streams.

    Randy.
    Randy Guest

  3. #3

    Default Re: open CONSOLE?

    perl.org wrote: 

    open( OUT, 'con' ); # IIRC

    I /think/ you can also use POSIX::fdopen() or IO::Handle::new_from_fd()
    to get the standard streams.

    Randy.
    Randy Guest

  4. #4

    Default Re: open CONSOLE?

    For windows try this

    sub executeTool($){
    my ($toolPath) = _;
    my $comspec = $ENV{'COMSPEC'}; # Get the path to the command line
    interpriter if this ENV var is not available in your system you will need to
    hard code this part

    open(TOOL, "$comspec /c $toolPath|");
    my $result = <TOOL>; # you will need to put this in a loop if the
    too gives multiple lines of output
    close(TOOL);
    return $result;
    }

    if your too going to give multiple lines of out put you will need to make
    some small changes






    LRMK
    ----- Original Message -----
    From: "Randy W. Sims" <org>
    To: "perl.org" <com>
    Cc: <org>
    Sent: Wednesday, June 23, 2004 3:59 PM
    Subject: Re: open CONSOLE?

     [/ref]
    call [/ref]
    a lot [/ref]
    that [/ref]
    open [/ref]
    redirected? 
    >
    > open( OUT, 'con' ); # IIRC
    >
    > I /think/ you can also use POSIX::fdopen() or IO::Handle::new_from_fd()
    > to get the standard streams.
    >
    > Randy.
    >
    > --
    > To unsubscribe, e-mail: org
    > For additional commands, e-mail: org
    > <http://learn.perl.org/> <http://learn.perl.org/first-response>
    >
    >[/ref]

    Lrmk Guest

  5. #5

    Default Re: open CONSOLE?

    In article <com>,
    com (Perl.Org) writes: 

    What makes you think that the output of the subprocess isn't going to be
    captured just because the parent's output is redirected? The subprocess's
    handles are created anew, not duped. A test shows it:

    sh-2.05a$ cat bar
    cat: bar: No such file or directory
    sh-2.05a$ perl -le 'print "STDOUT"; $x=`ls foo bar 2>&1`; warn "STDERR"; print $x' >foo 2>baz
    sh-2.05a$ cat foo
    STDOUT
    ls: bar: No such file or directory
    foo

    sh-2.05a$ cat baz
    STDERR at -e line 1.
    sh-2.05a$

    --
    Peter Scott
    http://www.perldebugged.com/
    *** NEW *** http://www.perlmedic.com/
    Peter Guest

Similar Threads

  1. Replies: 1
    Last Post: August 26th, 03:38 PM
  2. #25886 [NEW]: failed to open stream: Too many open files in Unknown on line 0
    By padair at pntsi dot ca in forum PHP Development
    Replies: 0
    Last Post: October 16th, 04:10 AM
  3. Replies: 5
    Last Post: October 6th, 06:08 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139