Reading config file with line break character?

Discussion in 'Perl Misc' started by Bernd Fischer, Nov 10, 2006.

  1. 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
    Bernd Fischer, Nov 10, 2006
    #1
    1. Advertising

  2. Bernd Fischer

    Andy Hassall Guest

    On Fri, 10 Nov 2006 18:03:31 +0100, Bernd Fischer
    <'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.andyh.co.uk
    http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
    Andy Hassall, Nov 10, 2006
    #2
    1. Advertising

  3. Thanks I got it.

    Bernd
    Bernd Fischer, Nov 10, 2006
    #3
  4. 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
    John W. Krahn, Nov 10, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wai Yip Tung
    Replies:
    5
    Views:
    1,107
    Andrew Dalke
    Sep 4, 2004
  2. M Irfan
    Replies:
    2
    Views:
    3,913
    M Irfan
    Apr 18, 2007
  3. Replies:
    7
    Views:
    3,570
  4. Matt

    line break character in javascript

    Matt, Mar 6, 2004, in forum: Javascript
    Replies:
    1
    Views:
    126
    Michael Winter
    Mar 6, 2004
  5. kampy
    Replies:
    9
    Views:
    323
    Steven D'Aprano
    Oct 19, 2012
Loading...

Share This Page