使用perl打印文本文件

使用perl打印文本文件,perl,printing,Perl,Printing,我是一个完全陌生的人,这应该是最容易做到的事情,但由于某些原因,我无法得到我的本地文本文件打印。在用不同的代码尝试了多次之后,我开始使用下面的代码,但是它没有打印出来 为了解决这个问题,我在不同的线程上搜索了好几天,但都没有运气。请帮忙。这是我的密码: #!/usr/bin/perl $newfile = "file.txt"; open (FH, $newfile); while ($file = <FH>) { print $file; } #/usr/b

我是一个完全陌生的人,这应该是最容易做到的事情,但由于某些原因,我无法得到我的本地文本文件打印。在用不同的代码尝试了多次之后,我开始使用下面的代码,但是它没有打印出来

为了解决这个问题,我在不同的线程上搜索了好几天,但都没有运气。请帮忙。这是我的密码:

#!/usr/bin/perl

$newfile = "file.txt";
open (FH, $newfile);

while ($file = <FH>) {
         print $file;
}
#/usr/bin/perl
$newfile=“file.txt”;
打开(FH,$newfile);
而($file=){
打印$file;
}
我将代码更新为以下内容:

#!/user/bin/perl

use strict;            # Always use strict
use warnings;          # Always use warnings.

open(my $fh, "<", "file.txt") or die "unable to open file.txt: $!";
                   # Above we open file using 3 handle method
                   # or die die with error if unable to open it.
while (<$fh>) {        # While in the file.
     print $_;     # Print each line
 }
close $fh;             # Close the file

system('C:\Users\RSS\file.txt');
#/user/bin/perl
严格使用;#始终使用严格的
使用警告;#始终使用警告。

打开(my$fh,“首先,在开始时,最好通过“使用警告”启用所有警告,并禁用所有可能导致不确定行为或难以通过pragma“使用严格”调试的表达式

在处理文件流时,始终建议检查您是否能够打开该流。因此,尝试使用croak或die都会使用给定消息终止程序

与其在while条件中读取,我建议检查文件的结尾。因此,在找到结尾时循环会中断。通常,在读取一行时,您会使用它进行进一步处理,因此最好使用chomp删除行的结尾

在perl中读取文件的示例如下:

#!/user/bin/perl

use strict;
use warnings;

my $newfile = "file.txt";
open (my $fh, $newfile) or die "Could not open file '$newfile' $!";
while (!eof($fh))
{
    my $line=<$fh>;
    chomp($line);
    print $line , "\n";
}
#!/user/bin/perl
严格使用;
使用警告;
my$newfile=“file.txt”;
打开(my$fh,$newfile)或死亡“无法打开文件'$newfile'$!”;
而(!eof($fh))
{
我的$line=;
chomp($line);
打印$line“\n”;
}

这很简单,包括解释

use strict;            # Always use strict
use warnings;          # Always use warnings.

open(my $fh, "<", "file.txt") or die "unable to open file.txt: $!";
                       # Above we open file using 3 handle method
                       # or die die with error if unable to open it.
while (<$fh>) {        # While in the file.
         print $_;     # Print each line
 }
close $fh;             # Close the file

这很可能是打开文件失败和检查
open
返回值失败的组合

如果您完全不熟悉perl,我强烈建议您阅读优秀的“perlintro”手册页,在命令行上使用
man perlintro
perldoc perlintro
,或者查看以下内容:

“文件和I/O”部分提供了一种很好且简洁的方法:

open(my $in,  "<",  "input.txt")  or die "Can't open input.txt: $!";

while (<$in>) {     # assigns each line in turn to $_
     print "Just read in this line: $_";
}
它们不会捕获打开文件的失败,但会在读取失败时发出警报

在这里的第一个示例中,您可以看到,如果没有诊断模块,代码将在没有任何错误消息的情况下失败。第二个示例显示了诊断模块如何更改此设置

$ perl -le 'open FH, "nonexistent.txt"; while(<FH>){print "foo"}'
$ perl -le 'use diagnostics; open FH, "nonexistent.txt"; while(<FH>){print "foo"}'
readline() on closed filehandle FH at -e line 1 (#1)
    (W closed) The filehandle you're reading from got itself closed sometime
    before now.  Check your control flow.
$perl-le'openfh,“nonexistent.txt”;while(){print“foo”}
$perl-le'使用诊断;打开FH,“nonexistent.txt”;while(){print“foo”}'
在-e第1行(#1)处关闭的文件句柄FH上的readline()
(W关闭)您正在读取的文件句柄在某个时候关闭了
在现在之前。检查您的控制流。
顺便说一句,传说中的“骆驼书”基本上是为纸质打印而格式化的perl手册页,因此按照
perldocPerl
中列出的顺序阅读perldocs将以一种合理易用且廉价的方式让您对该语言有一个高水平的理解


Happy hacking!

感谢您提供这段代码片段,它可能会提供一些有限的、即时的帮助。通过说明为什么这是一个很好的问题解决方案来正确解释它的长期价值,并使它对未来有其他类似问题的读者更有用。请您的答案添加一些解释,包括假设例如,为什么
use
die
语句很重要?为什么测试
eof()
比测试
$line
更好?这些东西对初学者来说并不明显,应该加以说明。我在本地文件中使用了这一点,但该文件仍然没有在cmd提示符中打印。我真的不知道出了什么问题,但我已经尝试打印了近10个小时。您在cmd上有任何输出吗?或者问题只是在读取文件?当我试图打开文件时,没有返回任何内容,因此我在最后添加了一个打印语句作为健全性检查…此打印语句确实打印了文件,但没有打印任何内容。这很奇怪,因为大约一周前我做了同样的事情,但没有问题拉入本地文本文件。此特定文件保存在本地,但它来自一个url。当我从url打开文件时,它打印得很好…只是不是来自本地文件。所以我创建了一个带有一堆文本的test.txt文件,并尝试打开和打印该文件,然后Zlich,nada,什么也不打印。
使用警告;使用autodie;
应该会有帮助。另外
使用strict;
也有很大的好处。你能解释一下吗请更新
文件.txt
文件中包含的内容。您的代码在我这边运行良好。好的,那么我不知道发生了什么,因为我的文本文件中有14000KB的数据,我无法将任何数据发送到print@R.Stone请试着一次坚持一个观点。你会带着评论走遍整个节目。我正在努力展示你可以在下面的答案中找到你的问题,评论。请按要求给我确切的输出,而不仅仅是它给了我类似的答案。好的,尝试了此线程上提供的所有答案,但我仍然无法打印该文件。我甚至创建了一个新文件,并尝试使用该文件,但什么都没有…它不会抛出错误,什么都没有文件中的g打印。我添加了一个打印“完成”\n";在最后,它打印得很好,但文件中没有任何内容。我完全不知道。@R.Stone
模具
应该给你一个错误,是什么?不,没有。它只是运行,然后在cmd提示符中出现一个新行,看起来像这样的C:\Users\RSS>@R.Stone那么你的
文件中没有任何内容。txt
?确保C:\Users\RSS\file.txt
里面有数据?@R.Stone好的。现在帮我一个忙。编辑你的问题,添加你调用脚本的方法,以及file.txt的一些内容和当前.pl文件的确切内容。谢谢。我有骆驼书和其他几本。我上周打开其他文件时没有问题,所以我有
open(my $in,  "<",  "input.txt")  or die "Can't open input.txt: $!";

while (<$in>) {     # assigns each line in turn to $_
     print "Just read in this line: $_";
}
use warnings; # or use the -w command line switch to turn warnings on globally
use diagnostics;
$ perl -le 'open FH, "nonexistent.txt"; while(<FH>){print "foo"}'
$ perl -le 'use diagnostics; open FH, "nonexistent.txt"; while(<FH>){print "foo"}'
readline() on closed filehandle FH at -e line 1 (#1)
    (W closed) The filehandle you're reading from got itself closed sometime
    before now.  Check your control flow.