Perl Config::Tiny->read() 不处理 CRLF
Perl Config::Tiny->read() doesn't handle CRLF
我在 Windows 10 下使用带有 Ubuntu 的 Perl。我想使用 Perl Config::Tiny 模块来读取文件名和其他配置数据。当我在 Linux 中读取在 Windows 下创建的配置文件时,它在值的末尾留下了 Carriage Returns。我目前通过在 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,
只需将 crlf
作为 "encoding" 传递即可。这将用作打开模式:
$Config = Config::Tiny->read( 'file.conf', 'crlf' ); # Neither ':' nor '<:' prefix!
另请参阅
我在 Windows 10 下使用带有 Ubuntu 的 Perl。我想使用 Perl Config::Tiny 模块来读取文件名和其他配置数据。当我在 Linux 中读取在 Windows 下创建的配置文件时,它在值的末尾留下了 Carriage Returns。我目前通过在 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,
只需将 crlf
作为 "encoding" 传递即可。这将用作打开模式:
$Config = Config::Tiny->read( 'file.conf', 'crlf' ); # Neither ':' nor '<:' prefix!