From 93306e5051340113e365a5f756015962122d3c32 Mon Sep 17 00:00:00 2001 From: Sergey Poznyakoff Date: Thu, 22 Aug 2019 09:40:14 +0300 Subject: 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. --- Makefile.PL | 1 - lib/Config/Parser.pm | 33 +++++++++++++-------------------- t/TestConfig.pm | 26 +++++++++++++++++--------- t/conf09.t | 20 ++++++++++++++++++++ 4 files changed, 50 insertions(+), 30 deletions(-) create mode 100644 t/conf09.t 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,21 +71,13 @@ 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); - open FILE, $file or croak "Can't open $file"; - my ($text, $data) = split /(?m)^__DATA__$/, , 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__$/, , 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,22 +32,25 @@ Key arguments: sub new { my $class = shift; local %_ = @_; - - my $file = new File::Temp(UNLINK => 1); - if (defined(my $text = delete $_{text})) { - print $file $text; - } else { - while () { - 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 () { + print $file $_; + } } + close $file; } - 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" => {}}}}}); + + + -- cgit v1.2.1