package MyPairList; use strict; use warnings; use feature 'state'; use Data::Dumper; use List::Util qw(all none); use Exporter 'import'; our @EXPORT = qw(pair_list_parse); sub pair_list_parse { my $string = shift; sub get_tokens { my $string = shift; my @tokens = $string =~ /([()]|[^()\s]+)/g; } my @tokens = get_tokens $string; sub parse_list { state @tokens = @_; my @list; while (my $token = shift @tokens) { if ($token eq '(') { push @list, parse_list(); } elsif ($token eq ')') { return \@list; } else { push @list, $token; } } return \@list; } my $list = parse_list @tokens; sub parse_nested_list { my ($list) = @_; my %hash; if (none {ref $_} @$list) { if (@$list == 1) { return $list->[0]; } elsif (@$list == 2) { return { $list->[0] => $list->[1] }; } else { return $list } } elsif (all {ref $_} @$list) { return [ map { parse_nested_list($_) } @$list ]; } else { my ($key, $ref, @remain) = @$list; die 'wrong key ', Dumper $key if ref $key; die 'wrong value ', Dumper $ref unless ref $ref; die 'redundant value', Dumper \@remain if @remain; $hash{$key} = parse_nested_list($ref); } return \%hash; } return parse_nested_list($list) } 1;
I finish a little script, partly achieve my goal, firstly parsed to a nested list, then parsed to pairs or arrays.use '( (a) (b))' to represent list which avoid ambiguity. list is ok, and it keep order. Iterate list first, because it can not get deep value only be chained hash ref.A little example as below.
(config( (a A) (b B) ((good) (bad)) (d e f) (a (a1 (a2 A))) ) )
parsed to
$VAR1 = [ { 'config' => [ { 'a' => 'A' }, { 'b' => 'B' }, [ 'good', 'bad' ], [ 'd', 'e', 'f' ], { 'a' => { 'a1' => { 'a2' => 'A' } } } ] } ];
Like perl do, the parse result is normally what people mean

In reply to Re: parse lisp style config by vincentaxhe
in thread parse lisp style config by vincentaxhe

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.