Skip to content

Instantly share code, notes, and snippets.

@bor
Last active December 13, 2015 21:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bor/4976288 to your computer and use it in GitHub Desktop.
Save bor/4976288 to your computer and use it in GitHub Desktop.
Find unused subs in project
#!/bin/bash
# Usage:
# ./perl_unused_subs.sh project_dir
# ./perl_unused_subs.sh project_dir another_project_dir_that_can_use_prev
# ACK_OPTIONS="-h" && ./perl_unused_subs.sh project_dir
INC_DIRS=$@
TMP_FILE="$$.pl"
if [[ $INC_DIRS == '' ]]; then
echo 'project/includes dir(s) required'
exit
fi
# find all .pl & pm and join in one file
find $INC_DIRS -name '*.pl' -or -name '*.pm' -exec cat '{}' ';' > $TMP_FILE
if [[ ! -e $TMP_FILE ]]; then
echo 'Error: Cant find tmp file!'
exit
fi
# fixes & strips
perl -i -p -e 's:^package :\n\npackage :g' $TMP_FILE
perl -i -p -e 's:^__END__:\n:g' $TMP_FILE
perl -i -p -e 's:^\s*#.+?\n::g' $TMP_FILE
subs=`grep -E "^sub " $TMP_FILE | cut -d' ' -f2 | sort -u`
res=()
for sub in ${subs}
do
grep ${sub} $TMP_FILE | grep -vq "sub ${sub}"
if [ "$?" == "1" ]
then
res=("${res[@]}" "${sub}")
fi
done
if [ ${#res[@]} == 0 ]
then
echo "All subroutines in $INC_DIRS appear to be used"
else
echo "In $INC_DIRS apparently unused:"
for sub in ${res[@]}
do
ack --env --type=perl $sub $INC_DIRS
done
fi
rm $TMP_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment