Subject: | 'package' bareword used as hash key is detected as package statement |
package Foo;
sub foo {
my %h = (package => 123);
}
When parsing the above code, you get two package statements. I've taken
t/10_statement.t and adapted it (file attached).
Subject: | 10a_package.t |
#!/usr/bin/perl -w
# Test the various PPI::Statement packages
use strict;
use lib ();
use File::Spec::Functions ':ALL';
BEGIN {
$| = 1;
unless ( $ENV{HARNESS_ACTIVE} ) {
require FindBin;
$FindBin::Bin = $FindBin::Bin; # Avoid a warning
chdir catdir( $FindBin::Bin, updir() );
lib->import(
catdir('blib', 'arch'),
catdir('blib', 'lib' ),
catdir('lib'),
);
}
}
# Load the code to test
BEGIN { $PPI::XS_DISABLE = 1 }
use PPI::Lexer ();
# Execute the tests
use Test::More tests => 3;
use Scalar::Util 'refaddr';
#####################################################################
# Tests for PPI::Statement::Package
{
# Create a document with various example package statements
my $Document = PPI::Lexer->lex_source( <<'END_PERL' );
package Foo;
sub foo {
my %h = (package => 123);
}
1;
END_PERL
isa_ok( $Document, 'PPI::Document' );
# Check that both of the package statements are detected
my $packages = $Document->find('Statement::Package');
is( scalar(@$packages), 1, 'Found 1 package statement' );
is( $packages->[0]->namespace, 'Foo', 'Package 1 returns correct namespace' );
}
1;