in reply to Re^2: Apache piping to perl
in thread Apache piping to perl

I think your problem is that this:

while( < $fh > )

...should be this:

while ( <$fh> )

The difference is that the one that works does not have spaces before and after "$fh". With the spaces in there, it's actually trying to use the filehandle for glob (not what you intended).

A few other recommendations.

Replies are listed 'Best First'.
Re^4: Apache piping to perl
by WiseGuru (Initiate) on Sep 08, 2007 at 04:24 UTC
    I took out the spaces with no sucess, i believe there might be some sort of problem with the pipe code, or how it is being sent from apache, i will look into the apache thing, got any other ideas about the pipe set-up? thanks www.arobcorp.com
      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}