in reply to modifying a text file on Win32

Duh!! Thanks!! Now I've moved the parenthesis, and I'm getting the following error now from STDERR: "Can't open blah.txt: No such file or directory at c:\console.pl line 19, <STDIN> line 1." Which is odd because I'm looking at the file right now - I'm testing this on Windows 98, and the file isn't set to Read Only, and I've tried closing blah.txt completely before running the script. I've tried moving the file from c:\blah.txt to c:\perl\bin\blah.txt (I'm invoking the file from the command line from c:\perl\bin via "perl c:\console.pl"), but it doesn't seem to matter.

Any ideas??

Thanks,

Glenn

Replies are listed 'Best First'.
Re: Re: modifying a text file on Win32
by tachyon (Chancellor) on Aug 13, 2001 at 09:10 UTC

    First you can drop the Windows centric paths and use / instead of \ Perl will supply Windows with the correct path delimiters for the platform automagically changing / into \ as required.

    The path is not however your problem. As you open the file for read/write you need to remember that at the end of the read you are at EOF so you need to seek the begining of the file and also truncate it to 0 unless you want to run the risk of getting crap at the end. I have added as die to the print CONFIG (remove the seek and truncate and you will get the error). This runs fine now. BTW don't open STDERR ">error.log" without including some path info. Even ./ for the current working directory makes it obvious where to look for this file. Also when you read data in from a file the newlines will still be there so you don't need to add more in your substitution

    open(CONFIG, "+<c:/blah.txt" ) or die "Can't open blah.txt: $!"; my @configfile = <CONFIG>; seek CONFIG, 0, 0; # go to start of file for overwrite truncate CONFIG, 0; # get rid of old file foreach my $configfile (@configfile) { $configfile =~ s/serverport 111$/serverport $port/; print CONFIG $configfile or die $!; } close(CONFIG); close(STDERR);

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      Thanks for your suggestions. However, using c:/blah.txt still gives me the "can't find file" error. I've also tried variations like the filename in all caps, capitalizing the drive letter, even trying /blah.txt, thinking perhaps perl will translate that to the root of my only hard drive. No such luck.

      Other thoughts??

      Thanks,

      Glenn