perl +打印到文件

时间:2010-07-14 16:10:27

标签: perl

以下脚本会打印一些字符。

如何将字符打印到文件(例如/ var / tmp / file)

耶尔

#!/usr/bin/perl 


@myNames = ('one', 'two', 'three' , 'A' , 'B' , 'C' , 'D' , 'E');

foreach (@myNames) {

print "$_\n";

} 

4 个答案:

答案 0 :(得分:2)

运行脚本时,只需将输出重定向到文件:

$ ./myscript.pl > /var/tmp/file

答案 1 :(得分:0)

#!/usr/bin/perl 


@myNames = ('one', 'two', 'three' , 'A' , 'B' , 'C' , 'D' , 'E');

open(OUT,">","/var/tmp/file") or die "Could not open the output file: $!";

foreach (@myNames) {

print OUT "$_\n";

} 

close(OUT);

答案 2 :(得分:0)

open(FILE, ">/var/tmp/file") || die "File not found";
print FILE @myNames;
close(FILE);

答案 3 :(得分:0)

#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use autodie qw(:all);

my @names = qw(one two three A B C D E);

{
    open my $fh, '>', '/var/tmp/file';
    foreach (@names) {
        print {$fh} "$_\n";
    }
}