Harvey Quamen wrote: > > Hello all: Hello,
> I'm working my way through "Learning Perl" (3rd ed) and got stuck in > ch 14 (Process Management, pages 201-202 actually, to be specific) > where the "date" command is is opened through a filehandle with a > piped open. Just toying around to see how it worked, I put it in a > loop and was surprised to see that it worked perfectly for the first > iteration but then I got an error on every subsequent iteration: > > Use of uninitialized value in concatenation (.) at line 11, <DATE> line 1. > > Line 11 is the print line, but if I swap the filehandle with $now = > `date` everything works great. > > #!/usr/bin/perl -w > use strict; > > my $now; > > open DATE, "date|" > or die "Darth Vader prevents me from piping from date: $!"; > > foreach (1..4) { > $now = <DATE>; This will read from "date|" until the end of file is reached and subsequent reads will return undef. Since date only outputs one line then that is all you can read from this file handle. $now = `date` executes the date command each time the expression is evaluated. See the I/O Operators section of the perlop document. > print "It's now $now\n"; > sleep (1 + rand 10); > } > > close DATE; You should test the success of close() when using pipes. close DATE or die "Darth Vader prevents me from closing pipe from date: $!"; John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]