diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2019-08-22 09:40:14 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2019-08-22 09:45:19 +0300 |
commit | 93306e5051340113e365a5f756015962122d3c32 (patch) | |
tree | e2aec1afe787e2126743a3bad852de6eae56ff5d | |
parent | dc4e27c823eca27c5fcf206778c6174b58a3b248 (diff) | |
download | config-parser-93306e5051340113e365a5f756015962122d3c32.tar.gz config-parser-93306e5051340113e365a5f756015962122d3c32.tar.bz2 |
Use mro to track descendant classes.
* Makefile.PL: Drop dependency on Class::Inspector
* lib/Config/Parser.pm (new): Use mro to track descendant classes.
(findsynt): Revert
* t/TestConfig.pm
(new): Test for existence of the DATA handle before trying to read
from it.
(canonical_lexicon): New method.
* t/conf09.t: New test.
-rw-r--r-- | Makefile.PL | 1 | ||||
-rw-r--r-- | lib/Config/Parser.pm | 33 | ||||
-rw-r--r-- | t/TestConfig.pm | 26 | ||||
-rw-r--r-- | t/conf09.t | 20 |
4 files changed, 50 insertions, 30 deletions
diff --git a/Makefile.PL b/Makefile.PL index a09417a..a156cd0 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -15,3 +15,2 @@ WriteMakefile(NAME => 'Config::Parser', 'Text::ParseWords' => '3.26', - 'Class::Inspector' => '1.30', 'Data::Dumper' => '2.135_06', diff --git a/lib/Config/Parser.pm b/lib/Config/Parser.pm index 6ca5bf7..ca2fcce 100644 --- a/lib/Config/Parser.pm +++ b/lib/Config/Parser.pm @@ -7,3 +7,3 @@ use Cwd qw(abs_path); use Text::ParseWords; -use Class::Inspector; +use mro; @@ -32,7 +32,8 @@ sub new { $self->lexicon({ '*' => '*' }); - my $subs = Class::Inspector->subclasses(__PACKAGE__); + my @cl = grep { $_ ne __PACKAGE__ && $_->isa(__PACKAGE__) } + reverse @{mro::get_linear_isa($class)}; my $dict; - if ($subs) { - foreach my $c (@$subs) { - if (my ($file, $line, $data) = findsynt($c)) { + if (@cl) { + foreach my $c (@cl) { + if (my ($file, $line, $data) = $c->findsynt) { my $d = $self->loadsynt($file, $line, $data); @@ -72,17 +73,9 @@ sub findsynt { $file .= '.pm'; - # Normally each loaded file has a corresponding entry in %INC. However, - # in perl 5.16.3 and below, a call to Class::Inspector->subclasses - # for Config::Parser::Package returns, among real classes the "class" - # Config::Parser::SUPER, which is apparently an alias to Config::Parser, - # except that it satisfies the isa('Config::Parser::Package') check and - # is not listed in %INC. The exists check below helps eliminate it. - if (exists($INC{$file})) { - $file = abs_path($INC{$file}) - or croak "can't find module file for $class"; - local ($/, *FILE); - open FILE, $file or croak "Can't open $file"; - my ($text, $data) = split /(?m)^__DATA__$/, <FILE>, 2; - close FILE; - return ($file, 1+($text =~ tr/\n//), $data) if $data; - } + $file = abs_path($INC{$file}) + or croak "can't find module file for $class"; + local ($/, *FILE); + open FILE, $file or croak "Can't open $file"; + my ($text, $data) = split /(?m)^__DATA__$/, <FILE>, 2; + close FILE; + return ($file, 1+($text =~ tr/\n//), $data) if $data; return (); diff --git a/t/TestConfig.pm b/t/TestConfig.pm index 08f5bae..9cf04ef 100644 --- a/t/TestConfig.pm +++ b/t/TestConfig.pm @@ -34,12 +34,15 @@ sub new { local %_ = @_; - - my $file = new File::Temp(UNLINK => 1); - if (defined(my $text = delete $_{text})) { - print $file $text; - } else { - while (<main::DATA>) { - print $file $_; + my $file; + + if (fileno(\*main::DATA)) { + $file = new File::Temp(UNLINK => 1); + if (defined(my $text = delete $_{text})) { + print $file $text; + } else { + while (<main::DATA>) { + print $file $_; + } } + close $file; } - close $file; @@ -49,3 +52,3 @@ sub new { $self->{_expected_errors} = $exp if $exp; - if (-s $file->filename) { + if ($file && -s $file->filename) { $self->parse($file->filename); @@ -72,2 +75,7 @@ sub canonical { +sub canonical_lexicon { + my $self = shift; + Data::Dumper->new([$self->lexicon])->Terse(1)->Sortkeys(1)->Useqq(1)->Indent(0)->Dump; +} + sub expected_error { diff --git a/t/conf09.t b/t/conf09.t new file mode 100644 index 0000000..c533747 --- /dev/null +++ b/t/conf09.t @@ -0,0 +1,20 @@ +# -*- perl -*- +use lib qw(t lib); +use strict; +use Test; +use ConfigSpec; +use ConfigSpec3; + +plan(tests => 2); + +my $c = new ConfigSpec; +my $c3 = new ConfigSpec3; + +ok($c->canonical_lexicon, + q{{"core" => {"section" => {"base" => {"default" => "null","mandatory" => 1},"number" => {"array" => 1,"re" => "^\\\\d+\\$"},"size" => {"re" => "\\\\d+(?:(?i) *[kmg])"}}},"load" => {"section" => {"*" => {},"file" => {"check" => "_check_abs_name","mandatory" => 1}}}}}); + +ok($c3->canonical_lexicon, + q{{"core" => {"section" => {"root" => {"mandatory" => 1}}},"dir" => {"section" => {"diag" => {},"store" => {},"temp" => {}}}}}); + + + |