【问题标题】:Perl Config::Tiny->read() doesn't handle CRLFPerl Config::Tiny->read() 不处理 CRLF
【发布时间】:2019-03-30 01:45:22
【问题描述】:

我在 Windows 10 下使用 Perl 和 Ubuntu。我想使用 Perl Config::Tiny 模块来读取文件名和其他配置数据。当我在 Linux 中读取在 Windows 下创建的配置文件时,它会将回车符留在值的末尾。我目前通过在 Linux 下制作配置文件的临时副本来解决这个问题。

有没有办法告诉 Config::Tiny->read() 打开带有行尾处理的配置文件来满足我的要求?

这是我当前代码的片段:

use Config::Tiny;
my $configfile = 'MyScript.ini';
# ; MyScript.ini file looks like:
# [MyScript]
# infilename=Dii.fwdata
# outfilename=Dii.1.fwdata
# logfilename=Dii.ReverseMerge.log
# someotherconfig=xyzzy

say STDERR "read config from:$configfile";
# Windows CRLF nonsense
if ( $^O =~ /linux/)  {
    `perl -pe 's/\r\n/\n/' < $configfile  >/tmp/$configfile `;
    }
my $config = Config::Tiny->read($configfile);
my $infilename = $config->{MyScript}->{infilename};
my $outfilename = $config->{MyScript}->{outfilename};
# ... etc,

【问题讨论】:

  • 如果您使用的是 Linux 的 Ubuntu 发行版,那么您使用的不是 Windows。 Unix 系统(如 Linux)期望文本文件以 LF 结尾,而不是 CRLF。即使同时在同一个机器上运行 Windows 也是无关紧要的。
  • @Ikegami,不相关,除了配置文件可能有 CRLF 行结尾,是在 Windows 系统上创建的。在 Windows 环境中运行意味着您必须牢记行尾。我发现能够在 Strawberry Perl 和 Ubuntu Perl 之间切换非常有用。
  • 它确实有 CRLF 的事实是相关的,但它很可能有 CRLF 的事实不是(即它对问题或答案没有影响)。如果您正在讨论向 Config::Tiny 添加功能,但您没有在问题跟踪器中发布,这将是相关的。

标签: perl config perl-io


【解决方案1】:

只需将crlf 作为“编码”传递。这将被用作打开模式:

$Config = Config::Tiny->read( 'file.conf', 'crlf' ); # Neither ':' nor '<:' prefix!

另见

https://metacpan.org/pod/Config::Tiny

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-05
    • 2013-12-08
    • 2013-02-12
    • 1970-01-01
    • 2016-04-27
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    相关资源
    最近更新 更多