Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

strictv -- how unstrict is your code?

by toolic (Bishop)
on Sep 05, 2013 at 15:03 UTC ( #1052586=CUFP: print w/replies, xml ) Need Help??

Time and again I inherit code which does not use strict and warnings. One of the first things I do is turn on strict to see how much work lies ahead for me to make it strict. If I'm lucky, I just get a few complaints. More typically, however, I get screens full of output scrolling by. This will give you a short summary of the variables instead of the normal verbose strict output. For example:
$ strictv $card : 3 $cardcounter : 2 $count : 2 $counter : 3 $i : 5 $number : 2 $suit : 2 $types : 2 @alreadyused : 2 @cards : 6 @iterations : 2 @suit : 2 12 variables

It's also handy for code that people post on PerlMonks.

=head1 NAME B<strictv> - How (un)strict is your Perl code? =head1 SYNOPSIS strictv file ... =head1 DESCRIPTION Compile (but do not run) a Perl file using the C<strict> pragma. Only variables are checked. Input is a file (or several files). Output is to STDOUT. Example: strictv =cut use warnings FATAL => 'all'; use strict; use List::Util qw(max); use English qw(-no_match_vars); for my $file (@ARGV) { $CHILD_ERROR = 0; my @errs = qx(perl -Mstrict=vars -c $file 2>&1); if ($CHILD_ERROR) { my %var; for (@errs) { if (/open perl script/) { print; } elsif (/ "([^"]+)" /x) { $var{$1}++; } } if (%var) { my $width = max(map {length} keys %var); for my $name (sort keys %var) { printf " %-${width}s : %d\n", $name, $var{$name}; } print "$file: ", scalar(keys %var), " variables\n"; } } }

Replies are listed 'Best First'.
Re: strictv -- how unstrict is your code?
by dolmen (Beadle) on Sep 11, 2013 at 15:26 UTC
    Next step: publish it on CPAN as App::strictv
      Thanks for the encouragement. I have a few modules on CPAN, but I'll have to figure out a few things first.

      Some questions for the crowd:

      • Are there any guidelines for creating CPAN scripts in the App:: space (as opposed to modules)?
      • Or, does anyone recommend specific App:: uploads to use as a good model?
      • Can anyone see any OS portability issues with my code? It works on unix-like platforms.
      • Any Perl version issues? I think strict output has been pretty consistent over the years.

        Instead of using qx(perl ...), you might want to use qx("$^X" ...), for the (rare, but confusing) case where perl and perl-5.22.0 are not the same executable and your script was executed with the latter.

        Other than that, I'm not aware of any guidelines for the App:: namespace and also don't know any good distributions to copy from.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://1052586]
Approved by Athanasius
Front-paged by Athanasius
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2018-06-18 10:23 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (109 votes). Check out past polls.