Hm.
if(open($fh, "-|")){ while( < $fh > ){ $temp.= $_; } close($fh); open($fh, ">> /LogsEM/Log.txt"); flock($fh, 2); print $fh "seg 1 ".$temp."\n"; flock($fh, 8); close($fh); } exit;

Where's the else to the if?

The expression

open $fh, "-|";

does an implicit fork, and the STDOUT of the forked process (a copy of the actual process) is connected to the lexical filehandle $fh.

The forked copy executes the rest of the script right after the fork, which is in the call to open(). fork() returns the process ID of the forked process in the parent, and zero in the child, that's how open returns the child PID if opening a pipe.

So in your code the parent enters the if () { } block whith the open() conditional, the child doesn't: it just does an exit(), which is not what you want.

Give your forked child some code to do:

my $pid = open $fh, "-|"; if ($pid) { # parent here. Read from child. print "read from child: $_" while (<$fh>); wait; # ...for the child to finish. } else { # child here. STDOUT connected to parent $fh print while <STDIN>; exit; # important, you don't want the child to execute # the statements after this block! } # parent continues ...

That said, what the heck are you trying to do? Read data from apache? Just read STDIN. Seems like you have an XY Problem.

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

In reply to Re^5: Apache piping to perl by shmem
in thread Apache piping to perl by WiseGuru

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.