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

Re: Re: Re: Re: Re: Super Find critic needed

by BrowserUk (Pope)
on Jun 30, 2003 at 16:34 UTC ( #270216=note: print w/ replies, xml ) Need Help??


in reply to Re: Re: Re: Re: Super Find critic needed
in thread Super Find critic needed

Okay. Here is an untested modification of the second part of your script that reads the file into an array, applies the regexes to the array and notes if any changes were made. If there were changes, creates an new file and writes the changed content to it, and finally renames the new file over the old file.

Files who do contains anything that needs modifying will be untouched and retain theor original modification timestamps, and the window for errors resulting from system failures is much reduced, though not completely eliminated.

foreach my $file (@files) { open( FILE, '<', $file ) or die "Couldn't open $file: $!"; @data = <FILE>; close( FILE ); my $modified = 0; # Assume no modification foreach (@data) { # Increment the flag if changes are made ++$modified if s/servername\.aa\.company\.zzzz\.com/NEWNAME.\c +om/gi; ++$modified if s/\bservername\.aa\.company\.com\b/NEWNAME\.com +/gi; ++$modified if s/\bservername\b/NEWNAME\.com/gi; } # If we made no modifications, # leave the original file as is if( $modified ) { # Create a new file for the modified data open( FILE, '>', "$file.new" ) or die "Couldn't create $file.n +ew: $!"; print FILE for @data; close(FILE) # Then rename the new file over the old file, # effectively deleting the old # There is still a window of opportunity for error # if the system crashes, but it is much smaller. rename "$file.new", $file; } } print "Total Count = $ct\n";

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller



Comment on Re: Re: Re: Re: Re: Super Find critic needed
Download Code
Re: Re: Re: Re: Re: Re: Super Find critic needed
by Anonymous Monk on Jun 30, 2003 at 18:29 UTC
    Thanks I will now give it a try.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2014-08-29 11:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (280 votes), past polls