Skip to content

Commit

Permalink
Add ->version method to PPI::Statement::Package.
Browse files Browse the repository at this point in the history
  This will return the version (if any) for packages declared like:

    package Foo v1.2;
    package Bar 0.9;

etc...
  • Loading branch information
wolfsage authored and wchristian committed Apr 23, 2016
1 parent 47fbee3 commit 9d6babd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
22 changes: 22 additions & 0 deletions lib/PPI/Statement/Package.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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<version> 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<file_scoped> method will
Expand Down
13 changes: 4 additions & 9 deletions t/ppi_statement_package.t
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) }
Expand Down

0 comments on commit 9d6babd

Please sign in to comment.