Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

WIN redirecting STDOUT and STDIN to new spawned CMD console

by LanX (Saint)
on Mar 27, 2023 at 17:52 UTC ( [id://11151268]=perlquestion: print w/replies, xml ) Need Help??

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

Hi

this question is related to Prompting for input inside IDE run (was Reading from STDIN) but I think it's worth its own thread

I want a Perl script to write errors via STDERR and receive signals in the first CMD window where it's started and show/get STDOUT/STDIN in a second CMD which was spawned.

For debugging/development.

That's not too unheard off, the second window is similar to a GUI app which writes errors into the original console.

Now I remember achieving similar things with Xterms and TTYs on Linux, but is it also easily possible with Win CMDs?

Searching for "redirecting STDOUT" is always only showing >file stuff for me.

Cheers Rolf
(addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
Wikisyntax for the Monastery

update

fixed confusion, wrote STDERR where I meant STDIN

Replies are listed 'Best First'.
Re: WIN redirecting STDOUT and STDIN to new spawned CMD console
by stevieb (Canon) on Mar 28, 2023 at 07:58 UTC
    "That's not too unheard off, the second window is similar to a GUI app which writes errors into the original console."

    This isn't exactly as easy as it sounds, especially given the difficulty of transferring environment from one console to another. In berrybrew, doing what you suggest means having one app (ie. GUI) communicating with a backend that also communicates to another app (CMD window) to display any output. That 'kind of' happens in debug mode, but only artificially.

    The GUI is typically started from the CMD, so in this case it is the backend that communicates to the other window, but one way or another, you have to start another shell if you want to trap it there. It's unfortunately not as easy as a tee

    If this problem could ever be solved especially if the environment could be carried from one CMD to another, I could remove a couple hundred lines of C# and make great efficiencies :)

    I have literally been waiting for a solution to this specific question for a long time. I just didn't know how to ask it.

      Thanks, I didn't know it's that complicated.

      My approach to Win is to mentally map concepts I know from Linux, even after years. And this doesn't always fit.

      I also kind of remembered that a child process is inheriting STD-IN/OUT/ERR from the parent.

      So one of my ideas was that probably it was possible than an app spawns a console as child with "cloned" STDIN and STDOUT, while STDERR is kept to the parent's console.

      Cheers Rolf
      (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
      Wikisyntax for the Monastery

Re: WIN redirecting STDOUT and STDIN to new spawned CMD console
by Corion (Patriarch) on Mar 28, 2023 at 07:25 UTC

    Do you really mean "CMD" as in cmd.exe or do you mean a new console window?

    If you mean a console window, see Win32::Console, which allows you to create a (second) console window. I think it doesn't readily have methods to redirect STDIN/STDOUT there, but that should be fairly easy with tied filehandles.

Re: WIN redirecting STDOUT and STDIN to new spawned CMD console
by harangzsolt33 (Chaplain) on Mar 29, 2023 at 03:37 UTC
    What if you redirect stdout to a temp file and then launch a separate perl program whose only job is to monitor this temp file and see if it changes. And if it does change, it reads the last 25 lines from the file and prints it to this terminal window. That way whatever gets written to stdout will be displayed in the terminal window.
      That would be a *very* roundabout fashion, but might achieve the goal. Perhaps better with a named pipe instead of a temp file...
        > would be a very roundabout fashion

        Well with variations it's just a classic.

        You start a second console running the application redirecting STDERR to a log file. The first console does a tail -f on the log-file

        With PowerShell in the first window at least that's technically feasible, it has a tail like mechanism. °

        > Perhaps better with a named pipe

        Not sure how hard it is to have named pipes on windows.

        Cheers Rolf
        (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
        Wikisyntax for the Monastery

        updates

        °) Get-Content -Path ".\err.log" -Wait

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-03-28 13:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found