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

Check your CPAN modules for use vars

by usemodperl (Beadle)
on Jun 25, 2018 at 22:37 UTC ( #1217408=CUFP: print w/replies, xml ) Need Help??

Edit: Since no one likes my joke about use vars this node has been amended to a more useful version of the program:

#!/usr/bin/perl ################################## # Check installed *CPAN* modules for COMMENTS! # # Because CPAN modules have a lot of COMMENTS! # # https://perlmonks.org/index.pl?node_id=1217408 # ################################################## use strict; use warnings; use Config '%Config'; use ExtUtils::Installed; for (split $Config{path_sep}, $ENV{PATH}) { $|++ if -x "$_/grep" } die "you need grep" unless $|; my $time = time; my $perl = ExtUtils::Installed->new; my @cpan = $perl->modules(); my @temp = (); for (@cpan) { my @cpan = $perl->files($_); push @temp, @cpan; } @cpan = grep /site.*\.pm$/, @temp; my $opt = 0; if (@ARGV) { $opt = 1 } else { print qq~Checking CPAN modules for COMMENTS in Perl versio~. qq~n $^V\n(Invoke with any arg to skip questions and gener~. qq~ate a list of modules.)\n\n~; print qq~The list may be big and printing progress makes i~. qq~t a bit slower.\nDefault: display progress, format outp~. qq~ut and print offending lines of code.\nPress return to ~. qq~start or n for no progress and list output. y/N~; chomp($opt = <STDIN>); $opt = 1 if $opt and lc $opt eq 'n'; } my $INC = join '|', @INC; my $acme = 0; my $grep = 0; for my $cpan (@cpan) { print "\rChecking: ","$acme\tFound: $grep\t" unless $opt; if(@_ =`grep '^#' $cpan`) { @_ = grep !/^#(pod|[\-=~\*]|[#]{5,}|\s*\n)/,@_; # FAKE next unless scalar @_; (my $name = $cpan) =~ s/($INC)//; $name =~ s,^/,,; $name =~ s,/,::,g; $name =~ s/\.pm$//; s/^\s+/ / for @_; $_{$name} = join "\n ", @_; $grep++ } $acme++ } if ($opt) { print "$_\n" for sort keys %_ } else { $perl = scalar keys %_; $time = time - $time; print qq~$perl CPAN modules (out of $acme) found with COMMENTS!\n~; print "$_:\n $_{$_}","-"x60,"\n" for sort keys %_; print qq~$perl CPAN modules (out of $acme) found with COMMENTS!\n~; print "That took $time secs (grep searched ",sprintf("%0d",$perl/$t +ime), " modules/sec).\n"; }


ORIGINAL:

I heard the news today:
    Removal of use vars

    The usage of use vars has been discouraged since the introduction of our in Perl 5.6.0. Where possible the usage of this pragma has now been removed from the Perl source code.

And had to find out how many modules are afflicted on my system (5.26.2):

905 CPAN modules (out of 4918) found with "use vars"!

Code for you:
#!/usr/bin/perl ###################################### # Check installed CPAN modules for use of "use vars" # # Because Perl 5.28.0 removes discouraged "use vars" # # https://perlmonks.org/index.pl?node_id=1217408 # ###################################################### use strict; use warnings; use autodie; use Config '%Config'; use ExtUtils::Installed; for (split $Config{path_sep}, $ENV{PATH}) { $|++ if -x "$_/grep" } die "you need grep" unless $|; my $t = time; my $m = ExtUtils::Installed->new; my @temp = $m->modules(); my @cpan = (); for (@temp) { my @x = $m->files($_); push @cpan, @x; } @cpan = grep /site.*\.pm$/, @cpan; my $opt = 0; if (@ARGV) { $opt = 1 } else { print qq~Checking CPAN modules for "use vars" in Perl version $^V ~. qq~(removed in Perl 5.28)\nhttps://metacpan.org/pod/release/XSAWYE~. qq~RX/perl-5.28.0/pod/perldelta.pod#Removal-of-use-vars \n(Invoke~. qq~ with any arg to skip questions and generate a list of modules.) \n +\n~; print qq~The list may be big and printing progress makes it a bit slow +er. Default: display progress, format output and print offending lines of code. Press return to start or n for no progress and list output. y/N~ +; chomp($opt = <STDIN>); $opt = 1 if $opt and lc $opt eq 'n'; } my $INC = join '|', @INC; my $n = 0; my $g = 0; for my $c (@cpan) { print "\rChecking: ","$n\tFound: $g\t" unless $opt; if (@_ = `grep 'use vars' $c`) { (my $f = $c) =~ s/($INC)//; $f =~ s,^/,,; $f =~ s,/,::,g; $f =~ s/\.pm$//; s/^\s+/ / for @_; $_{$f}=join"\n ",@_; $g++ } $n++ } if ($opt) { print "$_\n" for sort keys %_ } else { $m = scalar keys %_; $t = time - $t; print qq~$m CPAN modules (out of $n) found with "use vars"!\n~; print "$_:\n $_{$_}","-"x60,"\n" for sort keys %_; print qq~$m CPAN modules (out of $n) found with "use vars"!\n~; print "That took $t secs (grep searched ",sprintf("%0d",$m/$t), " modules/second).\n"; }
Output:

------------------------------------------------------------
XML::Twig:
 use vars qw($VERSION @ISA %valid_option);

 use vars qw( $weakrefs);

 use vars qw( %filter);
------------------------------------------------------------
XML::Twig::XPath:
 use vars qw($VERSION);
------------------------------------------------------------
YAPE::Regex:
 use vars '$VERSION';
------------------------------------------------------------
YAPE::Regex::Explain:
 use vars '$VERSION';
------------------------------------------------------------
905 CPAN modules  (out of 4918) found with "use vars"!
That took 31 secs (grep searched 29 modules/second).

STOP REINVENTING WHEELS, START BUILDING SPACE ROCKETS!CPAN 🐪

Replies are listed 'Best First'.
Re: Check your CPAN modules for use vars
by haukex (Canon) on Jun 26, 2018 at 10:17 UTC
    #!/usr/bin/perl ###################################### # Check installed CPAN modules for use of "use vars" # # Because Perl 5.28.0 removes discouraged "use vars" # # https://perlmonks.org/index.pl?node_id=1217408 # ######################################################

    I think you are misunderstanding the perldelta entry. It says (emphasis mine):

    The usage of use vars has been discouraged since the introduction of our in Perl 5.6.0. Where possible the usage of this pragma has now been removed from the Perl source code.

    vars is still very much present, just its usage by the Perl core has been removed. Note that the change is not listed in the "Incompatible Changes" section, and "Updated Modules and Pragmata" actually says "vars has been upgraded from version 1.03 to 1.04." See also:

    perl5/ $ git log --oneline --grep="use vars" v5.26.0..v5.28.0
    5069e57764 Update Filter::Util::Call to CPAN version 1.58
    1218f5ba79 Remove use vars from B::Deparse
    bc60657c80 configpm: fix duplicate 'our' declaration
    d8f3f638c2 [MERGE] Remove unnecessary use of 'use vars'
    9b78f2106b Document the removal of use vars from sources
    e64a0c479a Replace multiple 'use vars' by 'our' in regen.
    83461ff883 Replace multiple 'use vars' by 'our' in t
    3d3a0a8a5e Replace multiple 'use vars' by 'our' in utils
    fd55ca4fb1 Replace multiple 'use vars' by 'our' in ext
    cc01160e81 Replace multiple 'use vars' by 'our' in lib
    1a58b39af8 Replace multiple 'use vars' by 'our' in dist
    94d11354c7 fix a benchmarks sprintf entry

Re: Check your CPAN modules for use vars
by stevieb (Abbot) on Jun 26, 2018 at 22:07 UTC

    It's been already confirmed that you "misunderstood" the perldelta entry regarding use vars;, I just want to throw a piece of advice to you...

    It is advantageous to use variable names that are descriptive of the function they are being used for. It is very difficult to comprehend code that is full of variables that are single-letter, other than those that are pretty much iteration-type variables (or single-char vars that are used in an extremely small/tight context, but I'd still recommend against it).

    $c, $n, $f, $m, $g... even in your small few-line code script there, it becomes a bit overwhelming.

    Extra keystrokes matter when writing code, and they are especially important when reading code a couple of days, months or years later.

    At least in Perl, we don't have to define everything with a type, such as const char* thing (c) or nonsense like List<Thing> thing = new List<Thing>() (c#). We can just do my $thing. The my and the full name of the variable are very useful.

      Completely agree. tchrist's Style Guide has this to say on the subject and includes this absolute gem from Dominus:

      The appropriate length of a name is directly proportional to the size of its scope.

      which, together with tchrist's philosophy that clarity trumps length (to which I also subscribe) I think sums it up nicely. It's also worth pointing out for completeness that such matters of style apply to any names in source: subs, packages, constants, variables, etc.

      It's fair to say that this advice is perfectly generic and applicable not just to Perl.

      Excellent advice as usual stevieb, thanks for reminding me. I refactored with your lesson in mind and recycled the code to extract comments (which are actually interesting). Hopefully this version is easier to read:
      #!/usr/bin/perl ################################## # Check installed *CPAN* modules for COMMENTS! # # Because CPAN modules have a lot of COMMENTS! # # https://perlmonks.org/index.pl?node_id=1217408 # ################################################## use strict; use warnings; use Config '%Config'; use ExtUtils::Installed; for (split $Config{path_sep}, $ENV{PATH}) { $|++ if -x "$_/grep" } die "you need grep" unless $|; my $time = time; my $perl = ExtUtils::Installed->new; my @cpan = $perl->modules(); my @temp = (); for (@cpan) { my @cpan = $perl->files($_); push @temp, @cpan; } @cpan = grep /site.*\.pm$/, @temp; my $opt = 0; if (@ARGV) { $opt = 1 } else { print qq~Checking CPAN modules for COMMENTS in Perl versio~. qq~n $^V\n(Invoke with any arg to skip questions and gener~. qq~ate a list of modules.)\n\n~; print qq~The list may be big and printing progress makes i~. qq~t a bit slower.\nDefault: display progress, format outp~. qq~ut and print offending lines of code.\nPress return to ~. qq~start or n for no progress and list output. y/N~; chomp($opt = <STDIN>); $opt = 1 if $opt and lc $opt eq 'n'; } my $INC = join '|', @INC; my $acme = 0; my $grep = 0; for my $cpan (@cpan) { print "\rChecking: ","$acme\tFound: $grep\t" unless $opt; if(@_ =`grep '^#' $cpan`) { @_ = grep !/^#(pod|[\-=~\*]|[#]{5,}|\s*\n)/,@_; # FAKE next unless scalar @_; (my $name = $cpan) =~ s/($INC)//; $name =~ s,^/,,; $name =~ s,/,::,g; $name =~ s/\.pm$//; s/^\s+/ / for @_; $_{$name} = join "\n ", @_; $grep++ } $acme++ } if ($opt) { print "$_\n" for sort keys %_ } else { $perl = scalar keys %_; $time = time - $time; print qq~$perl CPAN modules (out of $acme) found with COMMENTS!\n~; print "$_:\n $_{$_}","-"x60,"\n" for sort keys %_; print qq~$perl CPAN modules (out of $acme) found with COMMENTS!\n~; print "That took $time secs (grep searched ",sprintf("%0d",$perl/$t +ime), " modules/sec).\n"; }
      STOP REINVENTING WHEELS, START BUILDING SPACE ROCKETS!CPAN 🐪
Re: Check your CPAN modules for use vars
by Anonymous Monk on Jun 25, 2018 at 23:19 UTC
    Because Perl 5.28.0 completely removes "use vars"

    no it hasnt, stop spreading FUD

      Seriously? Just misunderstood. Anyway slightly edited the description.
      STOP REINVENTING WHEELS, START BUILDING SPACE ROCKETS!CPAN 🐪
        Seriously, yes. You do such a good job of posting and reposting FUD.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://1217408]
Approved by Paladin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2018-12-11 08:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many stories does it take before you've heard them all?







    Results (53 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!