[英]How to create a new file in Perl?
I've some values stored in the variables $a
, $b
, $c
. 我在变量
$a
, $b
和$c
存储了一些值。 Now I've to load these values into new file (create file & load). 现在,我必须将这些值加载到新文件中(创建文件并加载)。 I'm new to Perl, how can I do it?
我是Perl的新手,我该怎么办?
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use autodie qw(:all);
my $a = 5;
my $b = 3;
my $c = 10;
#### WRITE ####
{
open my $fh, '>', 'output.txt';
print {$fh} $a . "\n";
print {$fh} $b . "\n";
print {$fh} $c . "\n";
close $fh;
}
#### READ ####
{
open my $fh, '<', 'output.txt';
my ($a, $b, $c) = <$fh>;
print $a;
print $b;
print $c;
close $fh;
}
You should read perlopentut and Beginner Perl Maven tutorial: Writing to files . 您应该阅读perlopentut和Perl Maven初学者教程:写入文件 。
另一个选择: File :: read_file
提供便捷的read_file
和write_file
函数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.