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

In capturing the output of a system() or backticks, there seem to be no newlines in between lines of output. I've ran into this problem using ps, tail, netstat, and several other apps. When I capture the output into an array, everything ends up jammed together in @arr[0], and when I capture to a scalar, there are no newlines to split on. Any help would be appreciated.

-Jerry
http://www.digilliance.net

Replies are listed 'Best First'.
Re: system() and backticks question
by mitd (Curate) on Sep 05, 2001 at 20:47 UTC
    Not sure how you coding it but:
    my @res = `ps -ef`; print @res;

    works. Newlines are preserved.

    mitd-Made in the Dark
    'My favourite colour appears to be grey.'

      Thanks! Apparently backticks were the answer. I can't seem to find my copy of the script in which I initially ran into the problem, but thanks for the help all the same. My goal was to learn a coding principle, rather than have the monks troubleshoot a single piece of code for me.. (Thinking along the "Teach a man to fish" lines)

      -Jerry
      http://www.digilliance.net

Re: system() and backticks question
by scain (Curate) on Sep 05, 2001 at 20:46 UTC
    Jerry,

    I hate to sound like a broken record, but some sample code would probably help, as we could then see what might be causing the problem. In general though, you should note that system doesn't return standard out, so you should only use backticks for that. System only returns the exit code. As for the problem you describe, I haven't experienced that on my linux boxes, so that is why the code snippet may help.

    Scott

      Jerry, I agree with Scott, lets see a sample of the output also
Re: system() and backticks question
by VSarkiss (Monsignor) on Sep 05, 2001 at 21:09 UTC

    Hmm... The fact that you're using @arr[0] rather than $arr[0] in your question makes me suspect you may not have the hang of arrays and slices (yet).

    As others have mentioned, post some code; it's much easier to answer specific questions than general ones.

      As far as my understanding of arrays, scalars and hashes goes, $arr[0] is correct from a syntactical standpoint, but from a functional standpoint @arr[0] seems virtually indentical.

      -Jerry
      http://www.digilliance.net

        I wouldn't make such statements when brother merlyn is around. ;-)

        The difference is a big one. An array slice (which is what @arr[0] is) provides a list context, whereas an array element provides a scalar context. For a graphic illustration, try this code:

        my @d = qw(this is a plain array); my @a; $a[0] = @d; print "The scalar got '$a[0]'\n"; @a[0] = @d; print "But the array slice got '$a[0]'\n";
        Many things in Perl will behave differently in scalar versus list context. If you continue to use @a[0] when you mean $a[0], you will have a surprise one day.

        OTOH, it's your code....

Backticks and newlines...
by Rhose (Priest) on Sep 05, 2001 at 21:21 UTC
    I frequently use Perl scripts with Oracle's SQL*Plus (please don't ask... DBI/DBD is out in this situation... *Grins*) Anyway, here is a sample foreach loop I use to process the output from the Unix SQL*Plus.

    foreach (split(/\n/,`$_SQLPLUS -silent /nolog \@$_TMPSCRIPT`)) { #-- Stuff }

    This code executes a temporary SQL script with SQL*Plus, splits the output based on a newline, and processes each line (one at a time) as $_.

    What I am trying to show is that newlines *ARE* preserved with backticks.