Subject: | Finding declared variables fragile and misses loop variables |
PPIx::EditorTools version 0.10 has a get_all_variable_declarations function which does not find
the "my" variable in a for loop (for my $var ...). Also, it's a bit flaky and tends to break in
unexpected ways. Test script and output below.
Cheers,
Ovid
#!/usr/bin/perl
use strict;
use warnings;
use Test::Most 'no_plan';
use PPI;
use PPIx::EditorTools;
diag "PPI version is $PPI::VERSION";
my $code = <<'END_OF_CODE';
use warnings;
foreach my $arg (@ARGV) {
print $arg;
}
END_OF_CODE
# Test finding variable declaration when on the variable
my $declarations;
lives_ok {
$declarations = PPIx::EditorTools::get_all_variable_declarations(
PPI::Document->new( \$code ) );
}
'We should be able to find variable declarations';
explain $declarations;
ok exists $declarations->{lexical}{'$arg'},
'... and we should be able to find loop variables';
$code = <<'END_OF_CODE';
foreach my $arg (@ARGV) {
print $arg;
}
END_OF_CODE
lives_ok {
$declarations = PPIx::EditorTools::get_all_variable_declarations(
PPI::Document->new( \$code ) );
}
'We should be able to find variable declarations';
explain $declarations;
ok exists $declarations->{lexical}{'$arg'},
'... and we should be able to find loop variables';
And the output:
08-findvariables....# PPI version is 1.213
ok 1 - We should be able to find variable declarations
# {
# 'dynamic' => {},
# 'lexical' => {},
# 'our' => {},
# 'package' => {}
# }
not ok 2 - ... and we should be able to find loop variables
# Failed test '... and we should be able to find loop variables'
# at 08-findvariables.t line 29.
not ok 3 - We should be able to find variable declarations
# Failed test 'We should be able to find variable declarations'
# at 08-findvariables.t line 42.
# died: Can't use string ("") as an ARRAY ref while "strict refs" in use at
/home/cpoe/perl5/lib/perl5/site_perl/5.8.5//PPIx/EditorTools.pm line 115.
# {
# 'dynamic' => {},
# 'lexical' => {},
# 'our' => {},
# 'package' => {}
# }
not ok 4 - ... and we should be able to find loop variables
1..4
# Failed test '... and we should be able to find loop variables'
# at 08-findvariables.t line 45.
# Looks like you failed 3 tests of 4.
Dubious, test returned 3 (wstat 768, 0x300)
Failed 3/4 subtests
Test Summary Report
-------------------
08-findvariables (Wstat: 768 Tests: 4 Failed: 3)
Failed tests: 2-4
Non-zero exit status: 3
Files=1, Tests=4, 0 wallclock secs ( 0.02 usr 0.00 sys + 0.15 cusr 0.01 csys = 0.18 CPU)
Result: FAIL
shell returned 1