Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

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: 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.


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

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

    What's my password?
    Create A New User
    [mz2255]: I'm trying to post a question to "seekers of perl wisdom" but always getting a permission denied when I want to submit. There are no links in my post...
    [ambrus]: again? someone complained about this just a few days ago (although eventually they could post)
    [choroba]: Can you post the question in mz2255's scratchpad?
    [ambrus]: Corion, if you're here, can you check the spam filter logs to see what's triggering this time?
    [ambrus]: Petroza had trouble posting yesterday, but has posted Issues Fetching URL with a variable token since.

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (9)
    As of 2017-10-19 15:23 GMT
    Find Nodes?
      Voting Booth?
      My fridge is mostly full of:

      Results (255 votes). Check out past polls.