Reading config file with line break character?

B

Bernd Fischer

Hi,

I have a config file with a key value pair

KEY = VALUE

which I read in with

while ( <CONFIG> ) {
chomp; # no newline
s/#.*//; # no comment
s/^\s+//; # no leading withespaces
s/\s+$//; # no ending withespaces
next unless length; # unless something to read
( $var, $value ) = split( /\s*=\s*/, $_, 2 );
$preferences{ $var } = $value;
}

I'm now looking for a solution to read something like

KEY = VALUE1 VALUE2 VALUE3 \
VALUE4 VALUE5

How to modify the regular expressions to accept '\' as a
intentionally linebreak character?

Thanks Bernd
 
A

Andy Hassall

I have a config file with a key value pair

KEY = VALUE

which I read in with

while ( <CONFIG> ) {
chomp; # no newline
s/#.*//; # no comment
s/^\s+//; # no leading withespaces
s/\s+$//; # no ending withespaces
next unless length; # unless something to read
( $var, $value ) = split( /\s*=\s*/, $_, 2 );
$preferences{ $var } = $value;
}

I'm now looking for a solution to read something like

KEY = VALUE1 VALUE2 VALUE3 \
VALUE4 VALUE5

How to modify the regular expressions to accept '\' as a
intentionally linebreak character?

Accumulate partial lines in a variable and go back around the loop. For
example:

my $line = '';
while ( <CONFIG> ) {
chomp; # no newline
s/#.*//; # no comment
s/^\s+//; # no leading withespaces
s/\s+$//; # no ending withespaces
next unless length; # unless something to read

if (s/\s+\\$/ /) { # multi-line expression
$line .= $_;
next;
}

# use $line in the split (may contain multiple previous lines)
( $var, $value ) = split( /\s*=\s*/, $line . $_, 2 );
$preferences{ $var } = $value;
$line = ''; # clear accumulated lines
}

Whether this does what you expect with multi-line expressions with blank lines
or comments in them is up to you. There's also probably more elegant ways of
doing it, perhaps by modifying the while() condition and pushing the partial
lines back onto the source of lines there.
 
J

John W. Krahn

Bernd said:
I have a config file with a key value pair

KEY = VALUE

which I read in with

while ( <CONFIG> ) {
chomp; # no newline
s/#.*//; # no comment
s/^\s+//; # no leading withespaces
s/\s+$//; # no ending withespaces
next unless length; # unless something to read
( $var, $value ) = split( /\s*=\s*/, $_, 2 );
$preferences{ $var } = $value;
}

I'm now looking for a solution to read something like

KEY = VALUE1 VALUE2 VALUE3 \
VALUE4 VALUE5


while ( <CONFIG> ) {
if ( s/\\\n// ) { # if line ends in \
$_ .= <CONFIG>; # append next line
redo; # and continue
}
s/#.*//; # no comment
s/^\s+//; # no leading whitespaces
s/\s+\z//; # no ending whitespaces including newline
next unless length; # unless something to read
my ( $var, $value ) = split /\s*=\s*/, $_, 2;
$preferences{ $var } = $value;
}




John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,055
Members
48,769
Latest member
Clifft

Latest Threads

Top