Created
February 11, 2018 21:06
-
-
Save trytone/2b94e82bd9b7b922f3508faf136abc62 to your computer and use it in GitHub Desktop.
Perl - count variables in source code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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