Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: questions regarding "+<"

by lightoverhead (Pilgrim)
on May 15, 2014 at 19:56 UTC ( [id://1086198]=note: print w/replies, xml ) Need Help??


in reply to Re: questions regarding "+<"
in thread questions regarding "+<"

choroba,

Thank you for your reply.

But your case actually proved my point of view:

your file "1" originally contains 5 'q' ('qqqqq'),after your program, it only left 4 'q' ('wqqqq').

this shows that the first 'q' was clobbered.

Replies are listed 'Best First'.
Re^3: questions regarding "+<"
by davido (Cardinal) on May 15, 2014 at 20:02 UTC

    Filesystems generally don't support an insert feature. "+<" gives you read/write access. It isn't a "prepend" or "insert" feature. Any changes made will write over what was there before, beginning at the current position within the file.

    If you want to prepend something to a file, the standard idiom is to rename the input file, create a new file by the original name of the input file, start copying the old (newly renamed) input file over to the new file, while adding your "insert" text at the appropriate point, and follow that insertion with a copy of the remainder of the source file. Then unlink the original source file. That's essentially what the -i Perl command line switch does.


    Dave

      Dave:

      "Filesystems generally don't support an insert feature. "+<" gives you read/write access. It doesn't create a "prepend" or "insert" feature. Any changes made will write over what was there before, beginning at the current position within the file." This is exactly I was looking for.

      I have checked camel book, but didn't find a detailed explanation for this behavior.

      Thank you.

        That's correct, there's not a detailed explanation in the Camel book describing that +< (which is based on fopen(3)'s "r+") overwrites rather than inserting. I suppose it's considered beyond the scope of the book to explain filesystem behavior, but it wouldn't hurt for clarification to be included.

        perlfaq5 has a FAQ question entitled "How do I change, delete, or insert a line in a file, or append to the beginning of a file?" I think that after reading that you will probably have fewer questions, and a more thorough understanding of how to insert without overwriting.


        Dave

Re^3: questions regarding "+<"
by choroba (Cardinal) on May 15, 2014 at 20:11 UTC
    Try the same with the ">" mode to see what clobbering means.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1086198]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2024-04-20 13:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found