in reply to Re^8: parse lisp style config
in thread parse lisp style config
Last time I just add my to two subs do recusion, and die with 'undeined sub', now I replace it with __SUB__, still 'Undefined subroutine &MyPairList::__SUB__ called at /usr/local/bin/MyPairList.pm line 23, <> line 1.' It did not work.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; my sub get_tokens { my $string = shift; my @tokens = $string =~ /([()]|[^()\s]+)/g; } my @tokens = get_tokens $string; my sub parse_list { state @tokens = @_; my @list; while (my $token = shift @tokens) { if ($token eq '(') { push @list, __SUB__->(); } elsif ($token eq ')') { return \@list; } else { push @list, $token; } } return \@list; } my $list = parse_list @tokens; my 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 { __SUB__->($_) } @$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} = __SUB__->($ref); } return \%hash; } return parse_nested_list($list) } 1;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^10: parse lisp style config
by hippo (Archbishop) on Nov 30, 2024 at 11:51 UTC | |
by ikegami (Patriarch) on Nov 30, 2024 at 17:19 UTC | |
by vincentaxhe (Scribe) on Dec 01, 2024 at 04:07 UTC | |
|
Re^10: parse lisp style config
by ikegami (Patriarch) on Nov 30, 2024 at 17:40 UTC | |
by vincentaxhe (Scribe) on Dec 01, 2024 at 06:53 UTC |