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

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on Performing action on one file at same time by two perl scripts

Replies are listed 'Best First'.
Re: Performing action on one file at same time by two perl scripts
by ikegami (Patriarch) on Jan 21, 2011 at 07:20 UTC

    This should vary by OS, by how you open the file, by when you flush the file, by how much you're writing out and by where you are writing in the file.

    Why don't you ask how to do what you want to do instead of asking such an open ended question?

    Update: Added more criteria.

Re: Performing action on one file at same time by two perl scripts
by Anonymous Monk on Jan 21, 2011 at 07:20 UTC
    Please let me know what will happen?

    T.I.T.S - Try It To See

Re: Performing action on one file at same time by two perl scripts
by jethro (Monsignor) on Jan 21, 2011 at 09:56 UTC
    In the worst case you will get garbage. That's why there are functions to lock files or even parts of files. Basically every script tries to lock a file for itself, if that fails it waits and tries again. Whoever has the lock writes to the file and then releases the lock.
Re: Performing action on one file at same time by two perl scripts
by JavaFan (Canon) on Jan 21, 2011 at 11:21 UTC
    Perform writing operation on x file by two perl scripts at the same time.

    Please let me know what will happen?

    What exactly will happen depends on how (and when) you open the file, how you write to it, where you write in the file, in which order you write, when you close the file, if and when you flush, if and when the library flushes, the phase of the moon, the current price of Brent Crude, and the number of airborne butterflies in Buenos Aires.
Re: Performing action on one file at same time by two perl scripts
by cdarke (Prior) on Jan 21, 2011 at 13:06 UTC
    With all the caveats of the comments above, including that it might not be allowed depending on the implementation, then it might be possible if the programs keep to their own area of the file.

    open the file for read and write ('+<'), then seek to a specific position, then write. Provided the two programs do not overlap the areas of the file they are writing then it might work. You might have to pre-create the file to the correct size before running the programs.

    I doubt it would be practical for text files with variable length lines.
Re: Performing action on one file at same time by two perl scripts
by lyklev (Pilgrim) on Jan 21, 2011 at 12:22 UTC
    You are assuming that you can actually open one file twice at the same time for writing. Some operating systems allow that, while others do not.