in reply to Re^5: parse lisp style config
in thread parse lisp style config

Just use already-mentioned my sub name { ... } instead or sub name { ... } and you will have an actual private inner sub without the drawbacks.

Replies are listed 'Best First'.
Re^7: parse lisp style config
by vincentaxhe (Scribe) on Nov 29, 2024 at 09:35 UTC
    thanks for emphasize it, I did overlook 'my sub ..', I did not know it is valid, I tried it, but for recursion subs it complains not defined.

      You haven't shown your code but it's a fair guess that you haven't used __SUB__ like the documentation tells you to do:

      my sub baz { __SUB__->(); # calls itself }

      🦛

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