2
votes

In Perl, I can open a child process and pipe its output to the calling Perl script, like this:

open(my $cmd, '-|', 'ls') or die $!;
while (<$cmd>) {
   print $_;
}

This prints the files in my working folder, e.g.:

> foo.txt
> bar.txt
> ...

But I would like to do the same thing for a child process that remains open, e.g. to pipe tcpdump's stdout to Perl, I attempt something similar:

open(my $cmd, '-|', 'tcpdump') or die $!;
while (<$cmd>) {
   print $_;
}

... but other than the tcpdump startup text, this doesn't mete out any http logs. It just seems to hang. What gives?

1
Suffering from buffering?mob

1 Answers

2
votes

It was buffering issues. I needed to add the -U flag to tcpdump. This causes packets to be written as soon as they're received.