2016-04-02 77 views
1

对Perl很新颖。在Padre和Windows 10 OS上运行Perl。perl新手,需要处理文件脚本的帮助

我书中的脚本是为Unix编写的。我不知道如何纠正它,以便它可以与Windows一起使用。

这是写在我的书(FOR UNIX)脚本:

use warnings; 
#write to a file with a filehandle. Sriptname: file.handle 
my $file="/home/jody/ellie/perl/newfile"; 
open(my $fh, ">", $file) || die "Can't open newfile: $!\n"; 

print $fh "hello world.\n"; 
print $fh "hello world again.\n"; 

在命令行

$perl file.handle 
$cat newfile 

输出应该看起来像这样:

hello world. 
hello world again. 

我做了如下修改但没有成功

use warnings; 
#Write to a file with a filehandle. Scriptname: file.handle 
my $file='C:\newfile.txt'; 
open (my $fh, ">", $file) || die "Can't open newfile: $!\n"; 

print $fh "hello world.\n"; 
print $fh "hello world again.\n"; 

当我运行脚本,我得到以下的输出:

can't open newfile: permission denied** 

当我与调试运行脚本,我得到了以下信息:

uncaught exception for user code 
can't open newfile: permission denied 
at handlingfiles.pl line 5 
press any key to continue 

我到底做错了什么?

+1

您的用户是否有权写入'C:\'文件夹? – choroba

回答

0

As @choroba在评论中提到,C:\newfile.txt将[试图]写入Windows根目录(例如C:\)。所以,你可能只需要newfile.txt

Cygwin的:如果您在使用附带cygwinperl,你也许可以使用/home/jody/newfile.txt因为这样的Perl支持POSIX文件语法。如果您在(例如)D:\cygwin处安装了cygwin,则/home目录将以D:cygwin\home结尾。请注意,您可以ls /home查看已定义的用户。

否则,如果您想要一个完整路径,那么.pl脚本的完整路径是什么。显然,你可以写入该目录。我一直在写perl很多年,在极少数情况下,当我在Windows上使用它时,我非常喜欢使用cygwin版本[也有许多脚本,工具等,像POSIX环境的功能]。 YMMV

0

Perl被要求打开文件进行写入,所以这就是它的作用。在Windows上,普通用户无法写入驱动器的根目录,并且Windows拒绝其请求。尝试类似C:\Users\User\My Documents\newfile.txt