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 | 19 | ||||
-rw-r--r-- | t/TestConfig.pm | 12 | ||||
-rw-r--r-- | t/conf09.t | 20 |
4 files changed, 36 insertions, 16 deletions
diff --git a/Makefile.PL b/Makefile.PL index a09417a..a156cd0 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -13,7 +13,6 @@ WriteMakefile(NAME => 'Config::Parser', PREREQ_PM => { 'Carp' => 0, 'Text::ParseWords' => '3.26', - 'Class::Inspector' => '1.30', 'Data::Dumper' => '2.135_06', 'File::Temp' => '0.22', 'Text::Locus' => '1.01', 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 @@ -5,7 +5,7 @@ use parent 'Config::AST'; use Carp; use Cwd qw(abs_path); use Text::ParseWords; -use Class::Inspector; +use mro; our $VERSION = "1.01"; @@ -30,11 +30,12 @@ sub new { $self->lexicon($lex); } else { $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); if ($d) { $dict = { %{$dict // {}}, %$d } @@ -70,13 +71,6 @@ sub findsynt { my $file = $class; $file =~ s{::}{/}g; $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); @@ -84,7 +78,6 @@ sub findsynt { 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 @@ -32,8 +32,10 @@ Key arguments: sub new { my $class = shift; local %_ = @_; + my $file; - my $file = new File::Temp(UNLINK => 1); + if (fileno(\*main::DATA)) { + $file = new File::Temp(UNLINK => 1); if (defined(my $text = delete $_{text})) { print $file $text; } else { @@ -42,12 +44,13 @@ sub new { } } close $file; + } my $exp = delete $_{expect}; # FIXME: Filter out fh and line keywords? my $self = $class->SUPER::new(%_); $self->{_expected_errors} = $exp if $exp; - if (-s $file->filename) { + if ($file && -s $file->filename) { $self->parse($file->filename); $self->{_status} = $self->commit; } else { @@ -70,6 +73,11 @@ sub canonical { return $self->SUPER::canonical(delim => ' '); } +sub canonical_lexicon { + my $self = shift; + Data::Dumper->new([$self->lexicon])->Terse(1)->Sortkeys(1)->Useqq(1)->Indent(0)->Dump; +} + sub expected_error { my ($self, $msg) = @_; 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" => {}}}}}); + + + |