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. | [reply] |
my sub baz {
__SUB__->(); # calls itself
}
| [reply] [d/l] [select] |
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. | [reply] [d/l] |