Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^13: Question about binary file I/O

by TheMartianGeek (Novice)
on Mar 05, 2011 at 15:25 UTC ( [id://891572]=note: print w/replies, xml ) Need Help??


in reply to Re^12: Question about binary file I/O
in thread Question about binary file I/O

So there is no single function that does that...well, that just sucks. I was hoping I could do it without needing seek(), but I guess if that's the only way, I don't have much of a choice. Isn't there a website somewhere where you can suggest features for the next version of Perl? That would be at least the second thing that I could think of.

Replies are listed 'Best First'.
Re^14: Question about binary file I/O
by BrowserUk (Patriarch) on Mar 05, 2011 at 15:33 UTC
    I was hoping I could do it without needing seek(),

    Why not just hide the seek inside a function of your own? Something like:

    sub printAt { my( $fh, $src, $nChars, $offset ) = @_; seek $fh, $offset, 0; return print $fh substr $src, 0, $nChars; } printAt( $fh, $s, 4, 1234 );

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Actually, that's a good idea. The problem with that, though, is that the print function completely erases the rest of the file. Now that I think about it, I don't know if I ever found a good way to overwrite characters in a file without erasing the rest of the file...
        The problem with that, though, is that the print function completely erases the rest of the file.

        It won't if you use the right open mode; ie. '+<':

        #! perl -sw use strict; ## Open for reading and writing open IO, '+<', 'junk.dat' or die $!; seek IO, 11, 0; print IO 'this overwrites'; close IO; __END__ C:\test>type junk.dat xxxxxxxxx xxxxxxxxx xxxxxxxxx xxxxxxxxx C:\test>junk59 C:\test>type junk.dat xxxxxxxxx this overwritesxxxxx xxxxxxxxx

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-25 19:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found