Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: (jeffa) Re: Unable to save changes using IO::File

by Anonymous Monk
on Sep 07, 2002 at 01:52 UTC ( [id://195820]=note: print w/replies, xml ) Need Help??


in reply to (jeffa) Re: Unable to save changes using IO::File
in thread Unable to save changes using IO::File

Jeffa-

Thanks for your reply and help but how can I modify my existing program? The reason being is that the ending program won't be as a simple as I have it. There will be numerous changes throughout.

Again thanks :-)

Replies are listed 'Best First'.
(jeffa) 3Re: Unable to save changes using IO::File
by jeffa (Bishop) on Sep 07, 2002 at 04:51 UTC
    erikharrison is dead on. In order for your existing program to work, you really have to open a temp file, write to it, then clob the first file by moving the temp file over the first file:
    use strict; use IO::File; my $in = IO::File->new; my $out = IO::File->new; my $change_on = "FChangeBar Yes"; my $change_off = "FchangeBar No"; # hmmm, lower case c? $in->open('test.mif') or die "can't read input"; $out->open('>tmp.mif') or die "can't write output"; while (<$in>) { s/$change_on/$change_off/g; print $out $_; } $in->close or die "can't close input"; $out->close or die "can't close output"; rename('tmp.mif','test.mif') or die "can't rename";
    Here is a much cooler version that uses Tie::File:
    use strict; use Tie::File; my @file; tie @file, 'Tie::File', 'test.mif' or die "cannot tie file"; s/(FchangeBar )Yes/$1No/g for @file;
    But, you might not be able to incorporate this into your ending program. At any rate, you are welcome and good luck! :)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (9)
As of 2024-03-28 10:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found