简体   繁体   English

如何在Perl中创建一个新文件?

[英]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 . 您应该阅读perlopentutPerl Maven初学者教程:写入文件

Have a look at the methods LoadFile and DumpFile of the YAML module. 看看YAML模块的LoadFileDumpFile方法。 They are very easy to use as you just need to throw a filename and the actual data against them. 它们非常易于使用,因为您只需要向它们抛出文件名和实际数据即可。

Ask specific questions if don't get along with these. 提出具体问题,如果不能与他们相处。

另一个选择: File :: read_file提供便捷的read_filewrite_file函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM