无法在 Windows 7 中使用 Perl 写入文件
我安装了 Strawberry Perl 并已经使用了一段时间。但是当我运行以下命令时,没有创建文件:
open TEST, ">happy.txt";
print TEST "contents";
close TEST;
我在任何地方都找不到答案。我想知道这是否是一个许可的事情。我以管理员身份运行 Perl 命令提示符并使用绝对文件路径。我在2台win7电脑上试过。 其他人可以在 Windows 7 上使用 Perl 创建文件吗?
两个指针(大概只适用于win7) - 由于 Windows 中的反斜杠,路径应该使用单引号?
- 看来应该使用完整路径。
这有效:
open my $test, '>', 'D:\_docs\documents\perl\happy.txt' or die $!;
print $test "contents\n";
close $test or die $!;
注意:使用文件的路径。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的代码本身没有任何问题会导致其失败。然而,它的问题在于它没有测试“open”和“close”的返回值是否成功。没有测试,你不知道故障点在哪里。如果您测试失败,您可能会更好地了解问题所在。
我将提供代码的更新版本,用于测试失败,并在失败时输出错误消息。当我这样做时,我将使用一些“最佳实践”,例如间接词法文件句柄和三参数打开:
以这种方式运行代码将更明确地告诉您失败是什么(而不是默默地失败) 。特殊变量 '$!'包含实际的错误消息。
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:
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.
我也找不到答案,也找不到答案。今天终于找到了这个解决方案,经过两天的长时间研究,在我自己的 64 位 Windows 7 上使用 ActiveState 的 32 位 Perl 5.16.1 build 1601 解决了同样的基本问题,下面的内容可以帮助我解决这个问题:为什么正常发布的语法似乎不起作用,我已经尝试了数十种排列,但直到今天才找到一种有效的方法 - 根据您的帖子和此处的答案。
下面是我的 Perl 示例 1 的输出消息
(其中我第一次没有零字节输出文件,并且还有一个包含我想要附加到那里的文本的文件。)
这工作得很好。
这是我的 Perl 示例 2:testme3.pl 第 85 行的关闭文件句柄输出上的 print() 的输出
。
示例 2:没有美元符号的 OUTPUT 变量。
和以前一样,当 print 语句无法将输出打印到文件,并且它再次显示而不是显示到屏幕上时,我收到了第一条错误消息。令我惊讶的是,我的打印语句试图在文件句柄已经关闭时打印!
在此之前,我没有收到来自操作系统的错误消息,也没有 die 消息,因为我没有在打印语句上放置 die 语句。下面是按相同顺序排列的两个示例代码片段,以显示产生上述消息的原因。顺便说一句 - 脚本使用严格;并使用警告;
示例 1:
示例 2:删除打印行上 OUTPUT 的 $ 前缀,给出上面有趣的新错误消息。
最后,这是新的非零输出文件及其内容:
c:\a000\myperl\projects\dir1>此处目录*
驱动器 C 中的卷没有标签。
卷序列号是 28B5-6AB9
c:\a000\myperl\projects\dir1 目录
12/15/2012 05:57 PM 22 这里OUTPUT.txt
2 个文件 22 字节
0 Dir(s) 636,297,977,856 字节可用
c:\a000\myperl\projects\dir1>在此处键入 OUTPUT.txt
这是创建的输出
c:\a000\myperl\projects\dir1>
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:
Example 2: Removing the $ prefix for OUTPUT on the print line, gave the interesting new error message above.
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>
在 Window (Server 2012) 上运行 perl5.24 (Strawberry) 和
open ... 时,我注意到类似的异常;打印$filehandle“某事”; close ...;
不起作用:以下不起作用(在 open 中使用 my 声明变量):
以下 DID 起作用(声明打开之前的变量):
另外,奇怪的是,我注意到文件没有更改,直到我
关闭$fh
。关闭文件句柄后,浏览器中的文件大小直接发生变化。
不知道原因是什么,但希望这个提示可以帮助并节省一些小时的尝试/失败
A similar anomaly I noticed when running perl5.24 (Strawberry) on Window (Server 2012) and
open ...; print $filehandle "something"; close ...;
didn't work:The following did not work (declaring the variable with my in open):
The follwing DID work (declaring the variable prior to open):
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