Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Check your CPAN modules for use vars

by stevieb (Canon)
on Jun 26, 2018 at 22:07 UTC ( [id://1217471]=note: print w/replies, xml ) Need Help??


in reply to Check your CPAN modules for use vars

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.

Replies are listed 'Best First'.
Re^2: Check your CPAN modules for use vars (identifiers)
by hippo (Bishop) on Jun 27, 2018 at 08:33 UTC

    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.

Re^2: Check your CPAN modules for COMMENTS
by usemodperl (Beadle) on Jun 27, 2018 at 11:23 UTC
    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 🐪

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-19 04:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found