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

I am tring to open a static file and read it all into a variable and then print that variable to another file. it doesn't seem to be working. here is the code
open (STATIC_HEADER, "<\./static_header.ps") ; open (OUT_FILE, ">$OUTPUT_PATH/$SUB_DIR/$NEW_FILE") ; @TMP_HEADER = <STATIC_HEADER> ; print @TMP_HEADER ; print OUT_FILE @TMP_HEADER ;
I do get what is in the array TMP_HEADER printed to my screen so i know it is opening the static file ok and reading it. But when i look at the out_file i don't have the contents of the array in it. can any one help me out. Thanks in advance

Replies are listed 'Best First'.
Re: Printing to a file problem
by ehdonhon (Curate) on Aug 22, 2002 at 15:56 UTC
    try some error checking to make sure everything is doing what you think:
    open (OUT_FILE,">$OUTPUT_PATH/$SUB_DIR/$NEW_FILE") or die ( $! );
      I know the file is opened also because i pting something to it afterwards and it is there.
      open (STATIC_HEADER, "<\./static_header.ps") ; open (OUT_FILE, ">$OUTPUT_PATH/$SUB_DIR/$NEW_FILE") ; @TMP_HEADER = <STATIC_HEADER> ; print @TMP_HEADER ; print OUT_FILE @TMP_HEADER ; print OUT_FILE "%%Page:" ;
      The page data is there but the static stuff is not
        Ok, then are you sure you are opening the STATIC_HEADER file like expected? I would put an  or die... in both of the opens to make sure. If it does not open @TMP_HEADER is empty and the print OUT_FILE @TMP_HEADER; does nothing.

        -Waswas
Re: Printing to a file problem
by sauoq (Abbot) on Aug 22, 2002 at 15:56 UTC
    Check to be sure that you are actually opening OUT_FILE.
    open (OUT_FILE, ">$OUTPUT_PATH/$SUB_DIR/$NEW_FILE") or die "$!";
    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Printing to a file problem
by CubicSpline (Friar) on Aug 22, 2002 at 16:01 UTC
    My guess is that you're actually not successfully opening your output file and you just aren't being careful enough to find out if that's the case. Try this:

    open (OUT_FILE, ">$OUTPUT_PATH/$SUB_DIR/$NEW_FILE") or die "$!";

    ~CubicSpline
    "No one tosses a Dwarf!"

Re: Printing to a file problem
by vek (Prior) on Aug 22, 2002 at 16:03 UTC
    Remember to always check to see if open commands are successful. I suspect the second open command failed and therefore nothing is written to the file.
    open (OUT_FILE, ">$OUTPUT_PATH/$SUB_DIR/$NEW_FILE") || die "Could not create blah blah blah - $!\n";
    -- vek --
Re: Printing to a file problem
by gnu@perl (Pilgrim) on Aug 22, 2002 at 19:41 UTC
    You may also have a buffering problem, try turning on autoflush with:
    $|++;
    This will let the data be written to the file immediately. It may be that you are closing your file handle before the data is actually written out.
      It may be that you are closing your file handle before the data is actually written out.

      Closing a filehandle automatically flushes anything left in the buffer.

      — Arien

        I seem to have mistated myself. What I meant to say is that the data has not yet been written to the file when he attempts to read from it and turning on autoflush would take care of that problem. You are correct about flushing on close. In the sample code here there is no close, so I must assume that he is trying to read from the file just after the write and before any close on the associated file handle.