Skip to content

Instantly share code, notes, and snippets.

@trytone
Created February 11, 2018 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trytone/2b94e82bd9b7b922f3508faf136abc62 to your computer and use it in GitHub Desktop.
Save trytone/2b94e82bd9b7b922f3508faf136abc62 to your computer and use it in GitHub Desktop.
Perl - count variables in source code
use Data::Dumper;
# usage: perl varcount.pl source.pl
open(X,$ARGV[0]);
my @lines = <X>;
close(X);
my $lineCount = 1;
my $variables = {};
foreach my $line (@lines){
while($line =~ /\$(\w+)/g){
my $scalar = $1;
if($scalar ne '_'){
$variables->{$scalar}->{count}++;
push(@{$variables->{$scalar}->{lines}},$lineCount);
}
}
$lineCount++;
}
print Dumper $variables;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment