Skip Menu |
 

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

This queue is for tickets about the PPIx-EditorTools CPAN distribution.

Report information
The Basics
Id: 63107
Status: resolved
Priority: 0/
Queue: PPIx-EditorTools

People
Owner: Nobody in particular
Requestors: ovid [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: 0.10



Subject: Finding declared variables fragile and misses loop variables
Download (untitled) / with headers
text/plain 2.6k
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
Download (untitled) / with headers
text/plain 846b
I forgot to add that as a very ugly local hack, I've rewritten the beginning of the function as follows to work around these bugs: sub get_all_variable_declarations { my $document = shift; my %vars; my %lexical; my $declarations = $document->find( sub { if ( $_[1]->isa('PPI::Statement::Compound') ) { if ( $_[1]->content =~ /\s*for(?:each)?\s+my\s+(\$[[:word:]]+)\b/ ) { $lexical{$1} = []; } } return 0 unless $_[1]->isa('PPI::Statement::Variable') or $_[1]->isa('PPI::Statement::Include'); return 1; }, ); $declarations ||= []; return {} unless @$declarations or keys %lexical; It's truly nasty, but it's worked. Cheers, Ovid
Download (untitled) / with headers
text/plain 357b
Hi Ovid, Thanks for the patch. I fixed it initially like this (but still did not handle for(each)? my ($foo, $bar)): http://padre.perlide.org/trac/changeset/13152 and I added your test in: http://padre.perlide.org/trac/changeset/13153 I am going to upload it soon as 0.11 once I complete the dzil-ification process :) Regards, Ahmad M. Zawawi azawawi
Fixed in 0.11 Thanks for your help, Ahmad M. Zawawi azawawi


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

Please report any issues with rt.cpan.org to rt-cpan-admin@bestpractical.com.