in reply to FILE READ and WRITE

In cases like this, you can get more detailed error messages by printing $! if certain builtin functions (like open or close) return false. Also, you probably want two different filehandles, since you're wanting to use both at the same time.

update: Oh, and close takes a filehandle for its argument.
Try something along these lines:

$output_file = "ConsumerMV.sh"; $input_file = "mikels.out"; $quit_code = "quit\n"; open(INFILE, $input_file) or die "Cannot read from input file: $!"; open(OUTFILE, ">$output_file") or die "Cannot write to output file: $! +"; while(<INFILE>){ chomp; print "Saw $_ in data.txt\n"; print FILEHANDLE $_ or warn "Can't print: $!"; } close INFILE or die "Can't close INFILE: $!\n"; close OUTFILE or die "Can't close OUTFILE: $!\n";

I believe I left (at least) one error in the code, but because of the added sanity checks, you'll now probably find it right away, as soon as you run the thing.

Replies are listed 'Best First'.
Re^2: FILE READ and WRITE
by ikegami (Patriarch) on Nov 22, 2005 at 23:05 UTC

    print FILEHANDLE $_ or warn "Can't print: $!";
    should be
    print OUTFILE $_ or warn "Can't print: $!";

    Also, it's kinda silly to keep looping after print returns an error, since you bothered to check the return value.

Re^2: FILE READ and WRITE
by mbayer (Novice) on Nov 22, 2005 at 20:42 UTC
    I guess you can see I haven't done much with Perl >:) -- Thanks for the info, didn't know you can change FILEHANDLES like that? Wonder what else I can change? Hhhmmmmm..... Thanks again. The Syntax kills me every time.