Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blank lines cause parsing to end prematurely #2

Closed
xtaran opened this issue Mar 1, 2020 · 0 comments · Fixed by #3
Closed

Blank lines cause parsing to end prematurely #2

xtaran opened this issue Mar 1, 2020 · 0 comments · Fixed by #3

Comments

@xtaran
Copy link
Contributor

xtaran commented Mar 1, 2020

This code snippet (reduced from my actual code) does not work as expected:

use ConfigReader::Simple;
my $default_config = <<'EOT';

# How often to poll in seconds
poll-interval = 3

EOT

$ConfigReader::Simple::Die = 1;
my $config = ConfigReader::Simple->new_string(
    Strings => [ \$default_config ],
    Keys  => [qw(poll-interval)],
    );

It bails out as follows:

Config: required keys [poll-interval] do not occur in config at … line ….

Reason seems to be this part of parse_string(), line 399 to 403:

        my @lines = split /\r?\n/, $$string;
        chomp( @lines );
#       carp "A: Found " . @lines . " lines" if $DEBUG;

        while( my $line = shift @lines ) {

If a line contains just \n or \r\n (which is chomped at line 400), $line just contains the empty string which is interpreted as false in boolean context. Hence, in the example above, parsing ends at the very first (empty) line, completely ignoring the remainder of the string.

But according to the POD, this should be possible:

If a line is only whitespace, or the first whitespace character is a #, the Perl comment character, "ConfigReader::Simple" ignores the line unless it is the continuation of the previous line.

(Besides, the "the first whitespace character is a #" should probably read "the first non-whitespace character is a #". Can open a separate issue or pull request for that if preferred.)

It seems as if using defined() around the assignment in line 403 fixes this:

        while( defined( my $line = shift @lines )) {

At least that fixed my case.

xtaran added a commit to xtaran/configreader-simple that referenced this issue Mar 1, 2020
Also slightmy modify test suite to also test this case by adding a
leading blank line to the tested example string.

Fixes briandfoy#2
xtaran added a commit to xtaran/configreader-simple that referenced this issue Mar 1, 2020
xtaran added a commit to xtaran/configreader-simple that referenced this issue Mar 1, 2020
Also slightly modify test suite to also test this case by adding a
leading blank line to the tested example string.

Fixes briandfoy#2
xtaran added a commit to xtaran/configreader-simple that referenced this issue Mar 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant