Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Reading config file with line break character?

Reply
Thread Tools

Reading config file with line break character?

 
 
Bernd Fischer
Guest
Posts: n/a
 
      11-10-2006
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
 
Reply With Quote
 
 
 
 
Andy Hassall
Guest
Posts: n/a
 
      11-10-2006
On Fri, 10 Nov 2006 18:03:31 +0100, Bernd Fischer
<(E-Mail Removed)'4054-50];p5.de> wrote:

>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.

--
Andy Hassall :: http://www.velocityreviews.com/forums/(E-Mail Removed) :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
 
Reply With Quote
 
 
 
 
Bernd Fischer
Guest
Posts: n/a
 
      11-10-2006
Thanks I got it.

Bernd
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      11-10-2006
Bernd Fischer wrote:
>
> 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
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
section with in a section config file and reading that config file kampy Python 9 10-19-2012 10:59 PM
Read a file line by line and write each line to a file based on the5th byte scad C++ 23 05-17-2009 06:11 PM
`if (!p ? i++ : 0) break;' == `if (!p){ i++; break;}' ? lovecreatesbea...@gmail.com C Programming 12 04-14-2008 07:59 AM
Reading config file other than web.config M Irfan ASP .Net 2 04-18-2007 01:11 PM
Reading a file line by line... in reverse Jeff Rodriguez C Programming 4 11-27-2003 09:50 AM



Advertisments