matze77 has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl use strict; use warnings; my $alter = "3"; #assign a value to alter different than 0 or 1 which is used later ... my $openfile = "$ENV{SystemRoot}/win.ini"; { open (MYFILE, ">>$openfile")or die "Can't open file 1: $!\n"; #open fo +r write, append not read! open (TEST, "c:/windows/win.ini") or die "Cant open file $!\n"; while(<TEST>) { if ( $_ =~ /[ARTEC]/ ) { my $alter = 1; } else { my $alter = 0; } } } if ($alter == 1) { #do something; print ".."; } close(MYFILE); close(TEST);
It seems like the while block never gets executed, any help?blabla [ARTEC] IniFile=f:\vaa\basis\vaa01.ini bla
use vars qw($change $alter); $alter = 0; sub readfile { while(<TEST>) { if ( $_ =~ /^\[ARTEC\]$/ ) { $alter = 1; exit print "found it exiting no need to change something"; }else { $change = 1; print "n "; } } return; } } if ($alter == 0 and $change == 1) { #do something ... print "changed\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: search and replace pattern inside file not working as expected
by Corion (Patriarch) on Jan 04, 2010 at 15:54 UTC | |
by matze77 (Friar) on Jan 04, 2010 at 16:19 UTC | |
|
Re: search and replace pattern inside file not working as expected
by toolic (Bishop) on Jan 04, 2010 at 15:59 UTC |