I want to read and write to an existing file using perl.
can any one tell me what is the mode that i have to use for such purpose:
I am quite confused by the below modes available.
I tried with +> but its not writing to the file.
mode operand create truncate
read <
write > ✓ ✓
append >> ✓
mode operand create truncate
read/write +<
read/write +> ✓ ✓
read/append +>> ✓
For eg:
i have a file like below:
one
two
four
i want to insert a line in between like :
one
two
three
four
Take a look at what
perlopentuthas to say about Mixing Reads and Writes. The conclusion is that none of the mixed-mode open modes are ideal, and the in-place edit mode (using$^I) is a better solution.For this particular problem I recommend the
Tie::Filemodule, which allows you to treat the file as a simple array.As an example, to insert the line
threeinto your example data, write