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

Is there any way to insert some data at the begginig of a file handle? For example, I have "FILE", but I want to put the contents of "$stuff" at the begging of FILE, because I have to give the filehandle to a function, to do stuff with it, or something..

Is that possible?

Thanks..

Replies are listed 'Best First'.
(atl) RE: Inserting stuff into a filehandle.
by atl (Pilgrim) on Aug 09, 2000 at 14:53 UTC
    That might depend on the operating system (at least I think that some old OSes with structured files types could behave totally different from Unix/Win).

    General remark: it is good idea to state the OS and Perl version (including used modules) if you ask a question.

    If you are using some sort of Unix, the answer is: no. You can seek any position inside the file, but if you write, you overwrite.

    Now, the question is, what do you want to do? If you generate some output (say, dynamic HTML) you might just buffer it into variables and write it out in one go. If your intermediate results have to be persistent (i.e. written to disk), you might consider storing them in seperate files and build your final file as needed. (Might be a good idea anyway, 'cause you don't manipulate earlier results which makes debugging easier.) If that is no option, too, I'm afraid you got to go with the tmp-file solution proposed by the other monks.

    Hope that helps a bit ...

    Andreas

    UPDATE:Merlyn pointed to the faq section on this topic when answering a similar question. I looked it up and there is a description on how to do it with temporary files: look here. Aaah, resourceful monastry ;-))

      I'm using linux, with perl 5.0
      I don't want to insert lines into a _file_, but into the filehandle. I'm getting data from STDIN, and I want to insert some data at the begginig before sending it to a MIME::Parser object. MIME::Parser takes the filehandle as a parameter.
Re: Inserting stuff into a filehandle.
by Anonymous Monk on Aug 09, 2000 at 12:15 UTC
    I'm quite sure this isn't possible without using an intermediate file.
(crazyinsomniac) Re: Inserting stuff into a filehandle.
by crazyinsomniac (Prior) on Aug 09, 2000 at 11:20 UTC
    I Think So.
    
    Easiest way i can think of is to have a TEMPFILE filehandle,
    and put the contents of "$stuff" and FILE to TEMPFILE,
    and then copy TEMPFILE to FILE.
    
    What might also work, but highly unlikely, is if you open a file handle for reading and writing,
    and set the $. variable to a negative value and do a print.

    I never tried this, and i'm not sure that $. can be set to a negative value.

    $.
    The current input line number for the last file handle from which you read (or performed a seek or tell on). An explicit close on a filehandle resets the line number. Because ``<>'' never does an explicit close, line numbers increase across ARGV files (but see examples under eof()). Localizing $. has the effect of also localizing Perl's notion of ``the last read filehandle''. (Mnemonic: many programs use ``.'' to mean the current line number.)

     ______________________________________________
    |_____¸.·ooO--(> cRaZy is co01. <)--Ooo·.¸_____|
     ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
      Setting $. does not do anything. It just changes the number that will get incremented next time you read a line. Perl doesn't use this number for anything, its there for the programmer to read, and reset. But nothing you put in it will affect the read operation.
Re: Inserting stuff into a filehandle.
by merlyn (Sage) on Aug 09, 2000 at 14:45 UTC