[H-GEN] Perl Question

Jason Parker-Burlingham jasonp at uq.net.au
Tue Nov 12 20:56:12 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:

> This is the print statement that I inserted to see why I wasn't entering the if 
> statement.

Unless you're using an earlier Perl, I can't see how those if
conditions are ever true at all; when I run a very similar script:

	#!/usr/bin/perl -w
	
	use strict;
	
	use IO::Select;
	
	# print terminal-safe random data
	open OD, "/usr/bin/od /dev/urandom |"
		or die "Can't start od: $!";
	
	# print "y" over and over again
	open YES, "/usr/bin/yes |"
		or die "Can't start yes: $!";
	
	# print disk statistics, four-second interval
	open IOSTAT, "/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) {
				print $fh,"\n";
			}
		}
	}

the only value returned from $select->can_read() is a string of the
form "GLOB(0x80fd4d0)".  This is just never ever going to match a
regular expression like /PROXY_PROC/.

Now, it _could_ be you're running an old Perl, and that this is the
right thing to do, but if you can redo things using the IO::Pipe
module, I'd go that route.

I guess the questions are (a) which version of Perl are you using, and
(b) does the program ever print "PROXY_PROC has data".

> the filehandle should contain data about the proxy processes but all
> it returns when read is 1.

I'm not sure how it could be possible but this looks a bit like a
context problem:

        $scalar = <FILH>;     # returns one line from the filehandle
        ($scalar) = <FILH>;   # returns one line, discards the rest
        @lines = <FILH>;      # returns every line at once.
        @array[0..2] = <FILH> # returns the first 3 lines, discards
                              # the rest

Ah:

	$ perl
	open F, "</etc/passwd";
	if ($o = <F> && (1 == 1)) {
	        print $o,"\n";
	}
	1

The problem is your if() syntax.  Use:

        $ perl
	open F, "</etc/passwd";
	if (($o = <F>) && (1 == 1)) {
	        print $o,"\n";
	}
	root:x:0:0:root:/root:/bin/bash

In the broken code, $o is set to the result of the expression "<F> &&
(1 == 1)" which is going to be 1 for all input that is true.  (If <F>
is not true, the if() won't be executed.)

> each increment of the command returns 1 ... weird when there is no
> errors.

Sadly, strictures and warnings don't help here:

	$ perl -w
	use strict;
	open F, "</etc/passwd";
	if (my $o = <F> && (1 == 1)) {
	        print $o,"\n";
	}
	1

You simply have to be attentive.  Writing code that is a little more
straightforward would have helped you spot this more quickly.

> Have already implemented some of your suggestions but this is just a
> proof of concept script for me and the industrial safeguards will
> appear in the production script.

Strictures and warnings actually help you write code *faster* by
tending to force you to use good programming practices.  They didn't
help in this case, but in a lot cases they will.

jason
-- 
||----|---|------------|--|-------|------|-----------|-#---|-|--|------||
| ``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