如何将内容重定向到文件而不是将其打印到终端

时间:2018-01-29 16:50:33

标签: perl

如何避免将内容打印到终端(如果在我的情况下它是20k行需要更多时间)而是将其重定向到perl中的文件?

这只是一个示例,而不是整个代码:

 if ($count eq $length)
                {
                    push(@List,$line);
                    print "$line\n"; #Prints line to terminal which is time consuming
                }

我在下面试过但是没有用

if ($cnt eq $redLen)
                    {
                        push(@List,$line);
                        print $line > "/home/vibes/text";
                    }

如果我的问题不明确,请告诉我?

2 个答案:

答案 0 :(得分:0)

只需使用3 argument open方法。

use strict;
use warnings;

my $line = "Hello Again!";
open (my $fh, ">", "/home/vibes/text") || die "Failed to open /home/vibes/text $!";
print $fh "$line\n";
close($fh); # Always close opened files.

答案 1 :(得分:-2)

perl中的默认文件句柄是STDOUT。您可以通过调用select来更改它:

print "Hello\n";          # goes to stdout

open my $fh, '>', '/home/vibes/text';
select($fh);

print "World\n";          # goes to file '/home/vibes/text'

在shell中,输出重定向通常是将> file附加到命令的问题。在Unix-y系统和Windows上都是如此。

$ perl my_script.pl > /home/vibes/text