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

Re: Tie::File with absolute path of the file

by haukex (Archbishop)
on Dec 05, 2017 at 06:13 UTC ( #1204924=note: print w/replies, xml ) Need Help??


in reply to Tie::File with absolute path of the file

In a recent thread I was sufficiently convinced by other monks that Tie::File - even though I still think it's kind of "neat" - is not a good choice in a serious application. On a large file it's much too inefficient, and even on a small file it's still more efficient to just read the entire file into an array and write it back out after modifying. So either:

open my $ifh, '<', $filename or die "$filename: $!"; chomp( my @array = <$ifh> ); close $ifh; # modify @array here open my $ofh, '>', $filename or die "$filename: $!"; print $ofh $_,"\n" for @array; close $ofh;

Or, to demonstrate a while(<>) loop and plug one of my own modules ;-) see File::Replace:

use File::Replace; my $repl = File::Replace->new($filename); my $infh = $repl->in_fh; while (my $line = <$infh>) { chomp($line); # modify $line here print {$repl->out_fh} $line, "\n"; } $repl->finish;

As for the problem you're having with Tie::File, I think both tangent and toolic have a very good point about write permissions. I would also add that perhaps using proper modules to handle file names platform independently might help with mitigating headaches about slashes etc. in filenames: either the core File::Spec, or, with a much nicer interface, Path::Class (there's also Path::Tiny with a similar interface, but be aware that that's restricted to Windows an *NIX).

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2022-08-16 10:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?