MadPogo has asked for the wisdom of the Perl Monks concerning the following question:

Okay I am certain I will confuse myself in tying to give an explanation so bear with me. I am attempting to create a single file that consists of several lines of header written from an array as well as appending several megs worth of tar file data after the header. I have tried the following and am certain I am missing something. The initial header information is written to the file but the tar data from the second file is never appended:
@header = ('BEGIN','WAFC','AREA_NAME','NODS','R','U','001','040','NONE +','200103231510','NONE','AREA_NAME','END'); if(open(OUTFILE, "> $temp_header") || die("Cannot Open File $temp_head +er for writing")) { foreach $line ( @header ){ print OUTFILE $line."\n"; print "$line \n"; } if(open(OUTFILE2, "< $sample_upload") || die("Cannot Open File $sa +mple_upload for reading")) { while (my $bytesread = read($sample_upload, my $buffer, 1024)) + { print OUTFILE $buffer; } } close (OUTFILE); close (OUTFILE2);
I am relatively new to Perl so any enlightenment you might provide would be greatly appreciated. MadPogo

Replies are listed 'Best First'.
Re: Multiple File Handles
by VSarkiss (Monsignor) on Oct 13, 2001 at 04:35 UTC

    Hmm... It looks like you're reading from the file name rather than the file handle. Your loop should look like this, I think:

    my ($bytesread, $buffer); # better to declare outside the loop while ($bytesread = read(OUTFILE2, $buffer, 1024)) { print OUTFILE $buffer; }
    I have to question the wisdom of calling a filehandle OUTFILE2, then reading from it....

    HTH

Re: Multiple File Handles
by stefp (Vicar) on Oct 13, 2001 at 04:51 UTC
    VSarkiss's remarks are correct. One additional point: it is a good thing to die with a message on a failed open like you do. Adding the magical variable $! will give the error system and make the message really helpful. It is good to know there is a failure at a given point, it is better to know the cause of the failure. For information on $!, see perlvar

    -- stefp