[H-GEN] Perl Question

Jason Parker-Burlingham jasonp at uq.net.au
Tue Nov 12 20:10:16 EST 2002


[ Humbug *General* list - semi-serious discussions about Humbug and     ]
[ Unix-related topics. Posts from non-subscribed addresses will vanish. ]

Scott Pullen <spullen at optusnet.com.au> writes:

>             $handle =~ /CPU_DISK/  && do { print "CPU_DISK has data\n\n";

Okay, so I cannot work out how this line will ever work.

Accoring to my documentation (version 1.14 of IO::Select and Perl
5.6.1)[1], IO::Select returns an array of handles (really GLOB
references since until recently you couldn't pass filehandles around
in Perl (hence the *FILH syntax)).

My solution has been to use more, not less, of the relatively new
IO::* hierarchy:

#!/usr/bin/perl -w

use strict;

use IO::Select;
use IO::Pipe;

# print terminal-safe random data
my $od = IO::Pipe->new->reader(qw"/usr/bin/od /dev/urandom")
	or die "Can't start od: $!";

# print "y" over and over again
my $yes = IO::Pipe->new->reader(qw"/usr/bin/yes")
	or die "Can't start yes: $!";

# print disk statistics, four-second interval
my $iostat = IO::Pipe->new->reader(qw"/usr/bin/iostat 4")
	or die "Can't start iostat: $!";

my $select = IO::Select->new();

$select->add($od, $yes, $iostat);

while (1) {
	while (my @readfhs = $select->can_read(0)) {
		foreach my $fh (@readfhs) {
			if ($od == $fh) {
				print "OD    :" . <$fh>;
			} elsif ($yes == $fh) {
				print "YES   :" . <$fh>;
			} elsif ($iostat == $fh) {
				print "IOSTAT:" . <$fh>;
			} else {
				die "Unknown filehandle returned from IO::Select object";
			}
		}
	}
}

Notice that in the if() block, one can now simply test for object
equality, and IO::Pipe takes care of all the forking and execing for
you, so you don't have to worry about shell metacharacters and such.

Try something along these lines and see if it helps you---although I
would probably pull out some of the repeated code.

Scott, I hope this is helpful for you---feel free to mail me privately
if you need more help.

(Oh, and one parting note:  is using 0 as an argument to
$select->can_read() acceptable?  Should you block or specify a more
reasonable period instead?)

Cheers,

jason

[1] : run
      $  perl -MIO::Select -le'print $IO::Select::VERSION'
      and 
      $ perl -V
      to see the version of IO::Select and Perl itself installed,
      respectively.
-- 
||----|---|------------|--|-------|------|-----------|-#---|-|--|------||
| ``It's just a big electric typewriter.''                              |
|                                                                       |
||--|--------|--------------|----|-------------|------|---------|-----|-|

--
* This is list (humbug) general handled by majordomo at lists.humbug.org.au .
* Postings to this list are only accepted from subscribed addresses of
* lists 'general' or 'general-post'.  See http://www.humbug.org.au/



More information about the General mailing list