Search code examples
perlconfigperl-io

Perl Config::Tiny->read() doesn't handle CRLF


I am using Perl with Ubuntu under Windows 10. I want to use the Perl Config::Tiny module to read filenames and other configuration data. When I read a config file created under Windows within Linux, it leaves the Carriage Returns at the end of the values. I currently get around this by making a temporary copy of the config file under Linux.

Is there a way to tell Config::Tiny->read() to open the config file with end of line processing that does what I want?

Here is a fragment of my current code:

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,

Solution

  • Just pass the crlf as the "encoding". This will then be used as the open mode:

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

    See Also

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