Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: Check a perl script for repeat "my" declarations

by robartes (Priest)
on Apr 01, 2003 at 09:40 UTC ( #247192=note: print w/ replies, xml ) Need Help??

in reply to Check a perl script for repeat "my" declarations

A slight, though still naieve, improvement would be to parse the output of B::Concise for padsv pad ops. Something like this:

use strict; my $script=shift; my @output=qx/perl -MO=Concise,-exec $script/; my $line=1; my %lexicals; foreach (@output) { if (/nextstate\([\w:]+ \d+ [\w.]+:(\d+)\)/) { $line=$1;next }; if (/pad[ahs]v\[(.[\w_]+):(\d+),\d+\]/) { unless ( exists ( $lexicals{$1}->{$2} ) ) { $lexicals{$1}->{$2}=[]; } push @{$lexicals{$1}->{$2}}, $line; } } foreach (keys %lexicals) { if ( scalar (keys %{$lexicals{$_}}) > 1 ) { print "Same lexical variable name in different scopes detected:\n" +; print " Variable $_ is used in:\n"; my $entry=$lexicals{$_}; print " Scope $_, line(s) ". join(',',@{$entry->{$_}})."\n" for k +eys %$entry; print "\n"; } }
When run on a script containing my $camel;{my $camel;my $frog='my $camel';}; this produces: syntax OK Same lexical variable name in different scopes detected: Variable $camel is used in: Scope 1, line(s) 5 Scope 2, line(s) 7
As seen, only real lexical declarations are detected, not string literals containing my. I have opted to print all lines containing the variable in the various scopes it appears in, but an easy change would be to just report the first line number it appears in, which should be the guilty declaration.

The code is tested, but it's definitely for fun only :)

Update: Changed regexp to also detect hash and array ops.


Comment on Re: Check a perl script for repeat "my" declarations
Select or Download Code
Re^2: Check a perl script for repeat "my" declarations (use B::Xref)
by Aristotle (Chancellor) on Apr 02, 2003 at 21:31 UTC
    I was going to propose a very similar angle, only I'd use B::Xref as it does 90% of what you need for this job. Its output is also more consistent and easier to parse for as this task.

    Makeshifts last the longest.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://247192]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (10)
As of 2014-07-25 12:29 GMT
Find Nodes?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:

    Results (171 votes), past polls