Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

DIY Tie

by yosefm (Friar)
on Sep 12, 2003 at 10:53 UTC ( [id://290986]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, all.

I've been trying to do my own file-tying mechanism. Yes, I know that <insert_favorite_module> does that and my laundry too, but how am I ever going to learn anything by using other people's work all the time, eh?

My problem right now is this: I'd like to delete a piece of data (say, an XML entity) from the middle of a file. Is there any way of doing that without re-writing the entire file? This just seems too wasteful in disk access times.

PS Though I'm looking for a DIY answer, I'll be also hapy to know what modules you are using for this kind of job.

Thanks!

Replies are listed 'Best First'.
Re: DIY Tie
by broquaint (Abbot) on Sep 12, 2003 at 11:05 UTC
    You will have to re-write some of the file as the position of the data will change, so it must be reflected on the disk (unless the changed data is the same length as existing data). To do a partial re-write you can just open a file for reading+writing with something like
    open( my $fh => '+<', $your_file ) or die "ack: $!"; ## read up to a point read_stuff() or last while <$fh>; ## remember where you were my $last_pos = tell $fh; my $rest_of_file = do { local $/; <$fh> }; $rest_of_file =~ s<$a_regex>($replace); ## go back to where you were seek $fh => $last_pos, 0; print {$fh} $rest_of_file; close $fh;
    While not a marvellous example, it should give you some idea of how to partially edit a file. See. open, tell and seek for more info.
    HTH

    _________
    broquaint

Re: DIY Tie
by Abigail-II (Bishop) on Sep 12, 2003 at 11:18 UTC
    I've been trying to do my own file-tying mechanism. Yes, I know that <insert_favorite_module> does that and my laundry too, but how am I ever going to learn anything by using other people's work all the time, eh?

    You actually might learn more from studying <insert_favorite_module> than asking here.

    Abigail

Re: DIY Tie
by BazB (Priest) on Sep 12, 2003 at 14:35 UTC

    Look at Tie::File, from dominus for inspiration (or just use it!).

    Tie::File has more than a few clever tricks up it's sleeve, including partial rewrites and deferred writes, which is probably what you want to do.


    If the information in this post is inaccurate, or just plain wrong, don't just downvote - please post explaining what's wrong.
    That way everyone learns.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-16 17:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found