Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Syntactic Confectionery Delight
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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:
mymy.pl 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.

CU
Robartes-


In reply to Re: Check a perl script for repeat "my" declarations by robartes
in thread Check a perl script for repeat "my" declarations by graff

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (13)
    As of 2014-04-18 21:32 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (472 votes), past polls