From 9d6babd4fd69cff3666d54605ff7977ae24b403e Mon Sep 17 00:00:00 2001 From: Matthew Horsfall Date: Fri, 22 Apr 2016 12:33:06 -0400 Subject: [PATCH] Add ->version method to PPI::Statement::Package. This will return the version (if any) for packages declared like: package Foo v1.2; package Bar 0.9; etc... --- lib/PPI/Statement/Package.pm | 22 ++++++++++++++++++++++ t/ppi_statement_package.t | 13 ++++--------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/lib/PPI/Statement/Package.pm b/lib/PPI/Statement/Package.pm index 52006ec8..865d237b 100644 --- a/lib/PPI/Statement/Package.pm +++ b/lib/PPI/Statement/Package.pm @@ -73,6 +73,28 @@ sub namespace { =pod +=head2 version + +Some package declarations may include a version: + + package Foo::Bar 1.23; + package Baz v1.23; + +The C method returns the stringified version as seen in the +document (if any), otherwise the empty string. + +=cut + +sub version { + my $self = shift; + my $version = $self->schild(2) or return ''; + $version->isa('PPI::Token::Structure') + ? '' + : $version->content; +} + +=pod + =head2 file_scoped Regardless of whether it is named or not, the C method will diff --git a/t/ppi_statement_package.t b/t/ppi_statement_package.t index a630637d..b7263fd5 100644 --- a/t/ppi_statement_package.t +++ b/t/ppi_statement_package.t @@ -56,15 +56,10 @@ END_PERL is( $packages->[1]->file_scoped, '', '->file_scoped returns false for package 2' ); is( $packages->[2]->file_scoped, 1, '->file_scoped returns true for package 3' ); is( $packages->[3]->file_scoped, 1, '->file_scoped returns true for package 4' ); - - TODO: { - local $TODO = "version functionality"; - - is( eval { $packages->[0]->version} , '', 'Package 1 has no version' ); - is( eval { $packages->[1]->version}, '', 'Package 2 has no version' ); - is( eval { $packages->[2]->version}, 'v1.23', 'Package 3 returns correct version' ); - is( eval { $packages->[3]->version}, '0.09', 'Package 4 returns correct version' ); - }; + is( $packages->[0]->version, '', 'Package 1 has no version' ); + is( $packages->[1]->version, '', 'Package 2 has no version' ); + is( $packages->[2]->version, 'v1.23', 'Package 3 returns correct version' ); + is( $packages->[3]->version, '0.09', 'Package 4 returns correct version' ); } my %known_bad = map { ( "package $_" => 1 ) }