perl buffering problem on input from shell command

Madison Kelly linux-5ZoueyuiTZhBDgjK7y7TUQ at public.gmane.org
Sun Mar 9 15:40:17 UTC 2008


Hi all,

   Another question, if I may. :)

   I've been struggling with a buffering problem... I listen to the 
output from a shell command ('dbus-monitor --system --profile', 
specifically). When certain strings come up, I need to perform some 
tasks. The problem is, the data coming in is cached, and so the loop can 
be delayed for some time.

   I figured it was the usual "suffering from buffering" issue, and 
tried various things I could to make the filehandle hot. Finally in 
reading some of the docs I realized that making a filehandle hot only 
works on output, not input. The only option I saw there was 'getc()', 
but I can't seen to get it working and it seems it will flush after 
every byte, where I want to flush on newline '\n'... Is this possible? 
Am I approaching this the wrong way entirely?

   Here is the exact code... (cleaned up for the post)

my $shell_call="dbus-monitor --system --profile 2>&1 |";
my $dm=IO::Handle->new();
my $child_pid=getc ($dm, $shell_call) or die "Error: $!\n";
$dm->autoflush(1);
push (@::pids, $child_pid);
while (<$dm>)
{
	chomp;
	my $line=$_;
	$line=~s/\s+/ /g;
	my ($type, $unix_time, $serial, $unknown1, $udi, $interface, 
$action)=split/ /, $line, 7;
	next if ( $type ne "sig" );
	next if ( $udi !~ /\/org\/freedesktop\/Hal/ );
	next if ( $udi =~ /acpi_BAT(\d+)$/ );
	
	# Go ahead and scan.
	&scan_devices($conf, $dbh);
}
$dm->close();

   Thanks!

Madi

--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list