Advertisement
DRVTiny

perlpf

Feb 25th, 2020
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.65 KB | None | 0 0
  1. #!/bin/bash
  2. declare -a tmp_files=()
  3.  
  4. doShowUsage () {
  5.         cat <<USAGE >&2
  6. Usage: $0 -[nx] <package> | -h  
  7. -n to show path to package which will be loaded by Perl in current/actual environment
  8. -x to turn on BASH trace mode
  9. -h to show this useful message
  10. USAGE
  11. }
  12. [[ $1 ]] || { doShowUsage; exit 1; }
  13. while getopts 'nxh' k; do
  14.     case $k in
  15.         n) flShowNative=1 ;;
  16.         x) export TRACE=1; set -x ;;
  17.         h) doShowUsage; exit 0 ;;
  18.     esac
  19. done
  20. shift $((OPTIND-1))
  21.  
  22. pckFQName=$1
  23.  
  24. to_be_cleaned () {
  25.         tmp_files+=($1)
  26. }
  27.  
  28. perl_native () {
  29.         local perlPackage=$1 resVarName=$2
  30.         local pckFile=${3:-${perlPackage//:://}.pm}
  31.         local ftmp
  32.         ftmp=$(mktemp /tmp/XXXXXXXXXXXXXXXXXXXXX)
  33.         to_be_cleaned $ftmp
  34.         local res=$(perl -M"${pckFQName}" -e 'print $INC{q<'${pckFile}'>}, "\n"' 2>"${ftmp}")
  35.         [[ $(stat -c %s "$ftmp") > 0 && $(<$ftmp) =~ locate\ ${pckFile//./\\.}\ in\ \@INC ]]
  36.         local rc=$((1 - $?))
  37.         (( $rc )) || eval "${resVarName}=\$res"
  38.         return $rc
  39. }
  40.  
  41. clean_on_exit () {
  42.         local f
  43.         for f in ${tmp_files[@]}; do
  44.                 rm -f "$f"
  45.         done
  46. }
  47.  
  48. trap clean_on_exit EXIT
  49.  
  50. perl_native $pckFQName pckFileFullPath || {
  51.         echo 'no such package found';
  52.         exit 1;
  53. }
  54.  
  55. if [[ $flShowNative ]]; then
  56.         echo "$pckFileFullPath"
  57. else
  58.         pckFile="${pckFQName//:://}.pm"
  59.         rxPackage="^.*/${pckFile//./\\.}$"
  60.  
  61.         find $(perl -e 'print join(" ", @INC)') \
  62.                 -type f \
  63.                 -regextype posix-extended -regex "${rxPackage}" \
  64.         2>/dev/null | sort | uniq
  65. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement