in reply to Re: Problem Modifying Files
in thread Problem Modifying Files

Hi Random:

Thanks for your help.

In answer to your questions: The original file was not unlinked. I'm sure the code was being called, as the "printf(" DemoQc descrip: $tmpVal \n tmpPthFile: $tmpPthFil\n");" lines were displayed. The ini-new file was updated.

Interesting issue, though . . . . I ran your code, changing only the "my $new="c:\\temp\\mytest";" line (so I'd be sure where the files were going), but nothing was created. I'm on Win2K, so I'm now wondering if there's something going on at a system level. I'm really grasping at straws with that one. Any ideas?

Thanks!

Replies are listed 'Best First'.
Re^3: Problem Modifying Files
by fglock (Vicar) on Sep 22, 2004 at 19:26 UTC

    You are using \\ in interpolation context, and then again:

    $new = "c:\\temp\\mytest"; # c:\temp\mytest -- ok so far open NEW, ">$new"; # c:<tab>emp<return>ytest -- oops

    Try it using non-interpolating, single quotes. For example:

    $new = 'c:\\temp\\mytest';
      Hi fglock:

      I'd agree if I were using single backslashes, but the double ones translate to a single backslash in the path. See the snippet below.

      Both ways yield the same results.

      Thanks for your help!

      #!/usr/bin/perl -w use strict; use File::Copy; my $new='c:\\temp\\mytest'; printf ("new = $new"); open NEW, ">$new" or die "I can not open file to write: $!\n"; print NEW "this is a test"; close NEW; << this yields the following on the CMD screen: >> new = c:\temp\mytest C:\Program Files\OptiPerl\webroot\cgi-bin>
Re^3: Problem Modifying Files
by Random_Walk (Prior) on Sep 23, 2004 at 08:25 UTC

    If you run the code in the temp dir then you can just leave the name without a path and remove concerns about handling of \\. I am on slowaris here so I can't test now but I think you can use c:/this/file/spec paths in win Perl and perl will handle the conversion for you, may be worth a go. I have also added some verbosity to the test to narrow down the failure. No time to test it here but it should run...

    #!/usr/bin/perl -w use strict; use File::Copy; my $new="mytest"; open NEW, ">$new" or die "I can not open file to write: $!\n"; print NEW "this is a test"; close NEW; if (-f "$new") { print "Created $new file just fine\n"; }else{ print "Did not create $new, most odd\n"; exit 1; } copy ("$new", "$new.new") or die "can't make a copy: $!\n"; if (-f "$new.new") { print "Copied $new to $new.new file just fine\n"; }else{ print "Could not copy $new, most odd\n"; exit 1; } unlink $new or die "can not unlink it: $!\n"; if (-f "$new") { print "Failed to delete $new\n"; exit 1; }else{ print "Deletion works too\n"; } rename ("$new.new", "$new") or die "cant move it: $!\n"; if (-f "$new") { print "Renamed $new.new to $new file just fine\n"; }else{ print "Rename failed, most odd\n"; exit 1; } unlink $new or die "can not unlink it at the end: $!\n"; if (-f "$new") { print "Failed delete second $new, that is realy odd\n"; exit 1; }else{ print "Deletion works again\nEverything looks fine\n"; }

    Cheers,
    R.