in reply to Config::General to read both scalars and arrays

use PPR; use Modern::Perl; use Data::Dumper; use Config::General; use Sub::Signatures; my $conf = Config::General->new( -ConfigFile => "pm5.ini", -Plug => { post_parse_value => sub ($name, $value) { $value = split_array_str( $value ) if looks_like_array( $value ) ; return 1, $name, $value; } } ); #naive implementation, excercise for the reader to improve #PPR might be useful to parse this, assuming Perl syntax for the #array sub split_array_str ($value) { return [split ",", substr($value, 1, -1)]; } #naive implementation, excercise for the reader to improve sub looks_like_array ($value) { return $value =~ m#^\[# && $value =~ m#\]$#; } my %conf = $conf->getall; print Dumper( \%conf );


holli

You can lead your users to water, but alas, you cannot drown them.

Replies are listed 'Best First'.
Re^2: Config::General to read both scalars and arrays
by holli (Abbot) on May 22, 2019 at 19:21 UTC
    I didn't know we have state variables now. I've been away reeeally long =)
    use Text::CSV; use feature 'state'; sub split_array_str ($value) { state $csv = Text::CSV->new ({ sep_char => ",", quote_char => '"', + allow_whitespace => 1 }); return $csv->parse( substr($value,1,-1) ) ? [$csv->fields] : $value; }


    holli

    You can lead your users to water, but alas, you cannot drown them.