0

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,
Wes
  • 423
  • 3
  • 12
  • If you're using the Ubuntu distribution of Linux, you're not using Windows. Unix systems such as Linux expect text files to be terminated with LF, not CRLF. That you are also running Windows on the same box, even if simultaneously, is irrelevant. – ikegami Oct 25 '18 at 19:15
  • @Ikegami, Irrelevant, except that the config file is likely to have CRLF line endings, having been created on a Windows system. Functioning within a Windows environment means that you have to keep the line endings in mind. I find it very useful to be able to switch between Strawberry Perl and Ubuntu Perl. – Wes Oct 25 '18 at 21:01
  • The fact that it does have CRLF is relevant, but the fact that it's likely to have CRLF is not (i.e. it has no effect on the question or the answers). It would be relevant if you were discussing adding a feature to Config::Tiny, but you didn't post in a issue tracker. – ikegami Oct 25 '18 at 21:02

1 Answers1

6

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

Corion
  • 3,855
  • 1
  • 17
  • 27