[comp.lang.perl] print

raymond@math.berkeley.edu (Raymond Chen) (12/19/89)

This arose when moving a script from perl2.0 to perl3.0.

% perl2 -e 'print (0?1:2),"foo\n";'              <- 2.0.1.8 Patchlevel 15
2foo
% perl -e 'print (0?1:2),"foo\n";'
2% perl -v
$Header: perly.c,v 3.0.1.2 89/11/17 15:34:42 lwall Locked $
Patch level: 6

If this is an intentional feature, I apologize.
--
 raymond@math.berkeley.edu         mathematician by training, hacker by choice

cjross@bbn.com (chris ross) (03/29/91)

I set out to write a TCP test client that reads from STDIN and multiple
sockets, and immediately ran into a nasty problem.  I have the following
code, running the degenerate case of only reading from STDIN.  (Error-
checking is removed for brevity.  Environment is perl 3.044 under SunOS
4.0.3)

for (;;)
{
    $bits = "";
    print("reading\n");
    foreach ('STDIN', keys(%Conns))
    {
	vec($bits, fileno($_), 1) = 1;
    }
    select($bits, undef, undef, undef);
    foreach ('STDIN', keys(%Conns))
    {
	next unless vec($bits, fileno($_), 1) == 1;
	print("got input from $_\n");			#  line 'x';
	sysread($_, $input, 1000);
	chop($input);
	print("got '", $input, "' from $_\n");		#  line 'y';
#	print("got '$input' from $_\n");		#  line 'z';
    }
}

I run this, and begin entering 'a', 'b', 'c', etc. on separate lines, as
test input.  As written, the script lags behind the input by one line; when
I enter 'c', it says "got 'b' from STDIN", and so forth.

If I comment out the line marked 'x', the script wakes up every five lines
of input, and prints the diagnostics for all five lines at once.  If I leave
line 'x' in, comment out 'y', and uncomment 'z', the script works as one
would expect.

Assuming that use of stdio on STDOUT was _somehow_ screwing up STDIN, I
wrote the following routine:

	sub Print
	{
	    local($s) = join('', @_);
	    syswrite(STDOUT, $s, length($s));
	}

and replaced the prints with &Prints.  It worked.  Then, on a lark, I
changed the Print routine to do a "print $s" instead of a syswrite, and
got quite a shock.  It still worked.

yarrrrrrrgh.
(help.)

chris ross         "We must not let our failures of imagination tell us what
<cjross@bbn.com>   must be the case in the universe." -- Patricia Churchland