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

Hello, I am trying to embed Perl in a C++ application. I have a window that takes input, when user hits return, the input is sent to Perl for evaluation. I then want to display the input back to them in the window and display it differently for error versus normal output. I've searched the web, but can't seem to find a clear example of how to do this in C. Presently, after calling result = perl_eval_pv() to eval the user input, I do SvPV(result, PL_sa) and the result goes to my window. The result is a code indicating success etc. However, this leaves stdout and stderr still going to the shell where my app was launched. I would like a pointer to an example of this kind of thing being done so I can emmulate it. Thanks. -William
  • Comment on Take over STDOUT / STDERR for Perl embedded in C application

Replies are listed 'Best First'.
Re: Take over STDOUT / STDERR for Perl embedded in C application
by Tanktalus (Canon) on Oct 05, 2005 at 01:20 UTC

    First thought is to do "wrap" the text to be evaluated like this:

    string s = "local *STDOUT; local *STDERR; open(STDOUT, \$Hidden::stdou +t) or die \"Can't redirect STDOUT: $!\"; open (STDERR, \$Hidden::stde +rr) or die \"Can't redirect STDERR: $!\";\n" + code_from_user;
    Then you should be able to access the scalars in the Hidden package.

    But, then again, I've never done any sort of XS code to interface with perl. I've thought about it, but I've never actually done it.

Re: Take over STDOUT / STDERR for Perl embedded in C application
by William_K_F (Initiate) on Oct 10, 2005 at 23:50 UTC
    The solution proposed did not work for me. I did succeed in C by reopening stdout and stderr before launching perl and sending them to a file. My gui then reads that file looking for new output. It's not the best solution, but it is working.