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

Hi Monks !

I'm sure some of you can help me with this - it seems like there must be a really simple solution, I just don't know it because of my inexperience ...

I'm using backticks to execute a command in my script, and the command (it's another perl script) is designed to output a whole heap of text to stdout (basically using bzcat).

I'm running my script and trying to capture the output of the backtick command, and feed that out through a pipe to the current stdout. Does that make sense ?

However, my problem is that whenever I run my script, I can see the command that I have called running on the system, but the text that it generates as output is never shown on my stdout, and so I'm unable to pipe it to the program I'm trying to run.

Can anybody help ?

I'm willing to paste code or explain myslef more clearly if needs be ... :->

Drew

  • Comment on lines written to stdout by a backtick command

Replies are listed 'Best First'.
Re: lines written to stdout by a backtick command
by sk (Curate) on Aug 15, 2005 at 06:19 UTC
    Here is a program that prints whatever you send in via the command line 10 times

    #!/usr/bin/perl -w # Program: printsth print join (' ', @ARGV), $/ for (1..10);

    Now I call this program inside another program and capture it's output

    #!/usr/bin/perl -w # Program: captpipe my $arg = "hi there"; print ("Going to call another perl program and capture it's output\n") +; open (PIPE,"printsth $arg | ") or die $!; while (<PIPE>) { print; }

    when you just run  printsth hi there you will get  hi there\n 10 times.

    when you execute the captpipe script you will see the same output.

    Output

    [sk]% captpipe Going to call another perl program and capture it's output hi there hi there hi there hi there hi there hi there hi there hi there hi there hi there

    this is a better way to capture output than using backticks!

      Yay! I used the open (PIPE,"blah") method and it worked!

      I had a feeling it was something to do with the depth and complexity of the piping and stdout reading that was causing it - using this different method must have corrected that.

      Thanks sk !

      Drew

        Yay! I used the open (PIPE,"blah") method and it worked!

        Seeing what you chose to omit in your shortened rendition of the method, I hope you realize that what matters here is not the name "PIPE" for the handle, but the | at the end of the second argument to open. I.e. it would have worked just as well if you had used

        open( THIS_IS_NOT_A_PIPE,"printsth $arg |" ) or die $!; while ( <THIS_IS_NOT_A_PIPE> ) { print; }
        (With apologies to Magritte.)

        the lowliest monk

Re: lines written to stdout by a backtick command
by GrandFather (Saint) on Aug 15, 2005 at 05:43 UTC

    When I run this:

    use warnings; use strict; print `Perl noname2.pl`;

    where noname2.pl contains:

    use warnings; use strict; print "This is noname2.pl\n";

    I get:

    This is noname2.pl

    which is what I would expect. What are you doing that is different?


    Perl is Huffman encoded by design.
      For a start, thanks for your help :->

      I wasn't using "print `blah`;", so I tried that, but it hasn't made any difference.

      The script I am writing is called by another Perl script, which expects my script to produce lines from web server logs (so lots and lots of output). My script calls another script which actually reads the web server logs and organises them chronologically, then outputs the lines to stdout.

      What I need is for my script to "pass through" the stdout output from the script I call, and output it on it's own stdout.

      So at the moment, I construct a command (as an array) from a number of variables concatenated together (and when I print this command without executing it, it looks correct), and then I was calling it using "system()", but I realised after looking in the Perl docs that system() will only return the exit code of the command it called, so that is obviously no use to me.

      Then I found that calling a command using backticks apparently lets you see the output of the command, but I must still be doing something wrong, as it doesn't.

      Drew

      Hang on, let me try that ...

      Drew

Re: lines written to stdout by a backtick command
by anonymized user 468275 (Curate) on Aug 15, 2005 at 11:04 UTC
    Although the backticks will work. It seems worth pointing out that they are not intended for running an additional perl script, but for collecting the output from a foreign process, e.g.:
    my $memout = `chcp`; $memout =~ /(\d+)/; print STDERR "Detected code page $1 ... continuing\n";
    Generally, if a piece of perl code should run subordinate to another then it belongs best as a subroutine in a module -- that is to say a module you maintain specifically for your applications, as opposed to a CPAN module.

    One world, one people

Re: lines written to stdout by a backtick command
by oceanic (Novice) on Aug 15, 2005 at 06:33 UTC
    By the way, I'm doing this on Windows, using Activestate Perl 5.8.0.

    I hope that's not the problem, as I have to run this on a production system under Windows Server 2003, and I'd rather not use Cygwin (which I have used with bash scripts instead to achieve the same results)

    Drew

      AFAIK, It should not cause any problems on windows... I tried the same script on my XP Pro SP2 machine and works just fine. I am on AS 5.8.7.

      On windows I couldn't get the shebangs to work well. If you have similar issues then you should change the open to  open (PIPE,"perl printsth $arg | ");

      cheers

      SK