简体   繁体   中英

Cannot write to file with Perl in Windows 7

I installed Strawberry Perl and have been using it for some time. But when I run the following, no file is created:

    open TEST, ">happy.txt";
    print TEST "contents";
    close TEST;

I can't find an answer anywhere. I wonder if it is a permission thing. I ran the Perl command prompt as administrator and played around with absolute file paths. I tried on 2 win7 computers. Can anyone else create files with Perl on windows 7?

Two pointers (prolly only apply to win7) -single quotes should be used for the path due to the backslashes in windows perhaps?
-It seems full paths should be used.

This worked:

open my $test, '>', 'D:\_docs\documents\perl\happy.txt' or die $!;
print $test "contents\n";
close $test or die $!;

Note: use the path to your file.

There is nothing inherently wrong with your code that would cause it to fail. What is wrong with it, however, is that it doesn't test the return value of 'open' and 'close' for success. Without testing, you don't know where the failure point is. If you test for failure you might get a better idea what is wrong.

I'll provide an updated version of your code that tests for failure, and outputs the error message upon failure. While I'm at it, I'll use some "best practices" such as an indirect lexical filehandle, and three-arg open:

open my $test, '>', 'happy.txt' or die $!;
print $test "contents\n";
close $test or die $!;

Running the code that way will tell you more explicitly what the failure is (as opposed to failing silently). The special variable '$!' contains the actual error message.

I could find no answer also, no where. After finally finding this solutoin today, after hours and hours of research on two days for the same basic problem on my own 64bit Windows 7 with ActiveState's 32bit Perl 5.16.1 build 1601, here is something below that sort of clears it up for me as to why the normal published syntax don't seem to work, and I've tried dozens of permutations but only today found one that works- per your posting and the answers here.

This below is the output message from my Perl Example 1

(where for the first time I have NO zero byte output file and also have one with text inside from what I wanted to append there.)

This worked fine.

This is the output from my Perl Example 2:

print() on closed filehandle OUTPUT at testme3.pl line 85. Example 2: with no dollar sign OUTPUT variable.

As before when the print statement failed to print output to the file, and it again showed instead displaying to screen, I had my first error message. To my surprise- my print statement was trying to print when the file handle had already closed!

Prior to that I had no error messages from the OS and no die messages either, since I did not put a die statement on my print statement. Here below are the two example code pieces in the same order to show what produced the above messages. By the way- the script had use strict; and use warnings;

Example 1:

print "\n\nThis worked fine";
my $OUTPUT;
open ($OUTPUT, '>hereOUTPUT.txt' ) or die "Can not create/open file. $!l";
print $OUTPUT "this is created OUTPUT";
close $OUTPUT;

Example 2: Removing the $ prefix for OUTPUT on the print line, gave the interesting new error message above.

print "\n\nExample 2: with no dollar sign OUTPUT variable.";
my $OUTPUT;
open ($OUTPUT, '>>hereOUTPUT.txt' ) or die "Ex3a) Can not create/open file. $!l";
print OUTPUT "this is non dollar appended OUTPUT";
close $OUTPUT;

And finally- here is the new non-zero output file and what's inside: c:\\a000\\myperl\\projects\\dir1>dir here* Volume in drive C has no label. Volume Serial Number is 28B5-6AB9

Directory of c:\\a000\\myperl\\projects\\dir1

12/15/2012 05:57 PM 22 hereOUTPUT.txt 2 File(s) 22 bytes 0 Dir(s) 636,297,977,856 bytes free

c:\\a000\\myperl\\projects\\dir1>type hereOUTPUT.txt this is created OUTPUT c:\\a000\\myperl\\projects\\dir1>

A similar anomaly I noticed when running perl5.24 (Strawberry) on Window (Server 2012) and open ...; print $filehandle "something"; close ...; open ...; print $filehandle "something"; close ...; didn't work:

The following did not work (declaring the variable with my in open ):

    open my $fh, '>', "c:/myfile";

The follwing DID work (declaring the variable prior to open ):

    my $fh;
    open $fh, '>', "c:/myfile";

Also It's strange, that I noticed, that the file did not change, until I close $fh . After closing the filehandle, the filesize directly changed in the browser.

Don't know what the cause for that is, but hope, this hint can help and save some hours of try/fail

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM