Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Inline: where did the output go?

by andye (Curate)
on Jun 12, 2007 at 10:24 UTC ( #620678=perlquestion: print w/replies, xml ) Need Help??
andye has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I'm using Inline::Pdlpp (similar to Inline::C) on a Unix platform.

Inside the C code, I print some stuff out using printf().

This works fine normally, but when I redirect output to a file, the output disappears. I.e. when I do:

./ > output.txt &
the file output.txt does contain the text that was printed from Perl, but doesn't contain anything printed from C. The C output doesn't appear on the terminal either.

So I'm a bit mystified. Any clues?

(My script doesn't mess with STDOUT at all, I'm not doing anything peculiar. I'm a pretty inexperienced C programmer though, so maybe I'm missing something there - I don't need to explicitly select STDOUT in C, do I? I'm guessing not as when the script is run without redirecting the output, everything is fine).

Thanks and best wishes, andye

Replies are listed 'Best First'.
Re: Inline: where did the output go?
by cdarke (Prior) on Jun 12, 2007 at 13:39 UTC
    My first thought was buffer flushing (fflush(stdout)) but I cannot see how a redirection to a file would affect that. In theory there should be no difference between writing to a screen and a file, unless the code calls something like istty(), or checks for screen attributes.
    Another possibility is that the shell you are running from is crashing, and never flushes the file buffer, but that is unlikely
    I recall some issues using printf from XS when I had to use the PerlIO layers, for example:
    PerlIO * debug = PerlIO_open ("debug.txt", "a"); PerlIO_printf(debug, "\nEntry, File: %s\n", szName);
    But I have no idea if you need this with your inline code. Can I suggest that you show a small sample of code?
    A run of strace or truss (with -f option), or ltrace if you have it, might clarify what is going on.
      Hi cdarke,

      I'm trying to work up a minimal example to show the problem - the issue I've got now is that when I make the example very minimal, the problem goes away. :)

      You may be right that it's a buffering problem though: I've written an example script which prints a line from Perl, then a line from C, then a line from Perl. On my Mac the lines appear in the file in the expected way, but on the Linux machine where I've originally found the problem, they appear in this order:
      1 Perl
      3 Perl
      2 C
      (should be 1 Perl, 2 C, 3 Perl)

      and putting a fflush(stdout) in the right place solves that problem...

      ...ok, so let's try doing that in the real script... success! Hooray!

      Triple++ (if I could) to cdarke, and many thanks.

      Best wishes, andye

        Hummm. Not sure I deserve that. The buffer should be flushed on exit from the program, regardless of whether it goes to the screen or a file.
Re: Inline: where did the output go?
by Moron (Curate) on Jun 12, 2007 at 12:16 UTC
    We don't know what you did in either program of course. My first instinct would be to try
    ./ > output.txt 2>&1
    and see if the output magically appears in the file. If so then you can conclude that the output is going to STDERR instead of STDOUT as you expected.

    Update: and if that doesn't do anything also try 0>&1 just to make sure and if that makes the difference you were writing to STDIN.


    ^M Free your mind!

      Tried that, but it didn't help. Thanks for the thought though.

      Best wishes, andye

Re: Inline: where did the output go?
by sen (Hermit) on Jun 12, 2007 at 10:59 UTC

    Instead of using

    ./ > output.txt &

    try this

    ./ >> output.txt &
      Hi sen - I'm not clear why appending would help? Anyway, just tried it and it didn't. Thanks for the thought though.

      Best wishes, andye

Re: Inline: where did the output go?
by Grundle (Scribe) on Jun 12, 2007 at 17:48 UTC
    It may be helpful for you to post the pdlpp block code so that we can determine why it wouldn't be working. As far as I know there can be two problems with what you are doing.

    1) You are using Inline::Pdlpp incorrectly, so maybe your C code isn't even being evaluated (hence no printing).

    2) The C code in the Pdlpp block is incorrect which is why nothing prints. This case does not make sense to me since you already tested that code independently from adding it to the block, which would mean you are quite sure of its correctness and of course is always a good practice, right?

    Anyways, with a little more info and testing I think we may be able to correctly diagnose the illness ;)
      oops disregard, I just noticed after I posted, you posted resolution :P cheers ;)
        Thanks anyway. ;)

        Best wishes, andye

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://620678]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2018-06-18 21:48 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (110 votes). Check out past polls.