开发者

perl + printing to file

开发者 https://www.devze.com 2023-01-07 08:25 出处:网络
The following script print some characters. how to print the characters to file ( f开发者_StackOverflow社区or example /var/tmp/file )

The following script print some characters.

how to print the characters to file ( f开发者_StackOverflow社区or example /var/tmp/file )

Yael

#!/usr/bin/perl 


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

foreach (@myNames) {

print "$_\n";

} 


When you run the script, you can simply redirect the output to a file:

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


#!/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);


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


#!/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";
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消