Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- declare -a tmp_files=()
- doShowUsage () {
- cat <<USAGE >&2
- Usage: $0 -[nx] <package> | -h
- -n to show path to package which will be loaded by Perl in current/actual environment
- -x to turn on BASH trace mode
- -h to show this useful message
- USAGE
- }
- [[ $1 ]] || { doShowUsage; exit 1; }
- while getopts 'nxh' k; do
- case $k in
- n) flShowNative=1 ;;
- x) export TRACE=1; set -x ;;
- h) doShowUsage; exit 0 ;;
- esac
- done
- shift $((OPTIND-1))
- pckFQName=$1
- to_be_cleaned () {
- tmp_files+=($1)
- }
- perl_native () {
- local perlPackage=$1 resVarName=$2
- local pckFile=${3:-${perlPackage//:://}.pm}
- local ftmp
- ftmp=$(mktemp /tmp/XXXXXXXXXXXXXXXXXXXXX)
- to_be_cleaned $ftmp
- local res=$(perl -M"${pckFQName}" -e 'print $INC{q<'${pckFile}'>}, "\n"' 2>"${ftmp}")
- [[ $(stat -c %s "$ftmp") > 0 && $(<$ftmp) =~ locate\ ${pckFile//./\\.}\ in\ \@INC ]]
- local rc=$((1 - $?))
- (( $rc )) || eval "${resVarName}=\$res"
- return $rc
- }
- clean_on_exit () {
- local f
- for f in ${tmp_files[@]}; do
- rm -f "$f"
- done
- }
- trap clean_on_exit EXIT
- perl_native $pckFQName pckFileFullPath || {
- echo 'no such package found';
- exit 1;
- }
- if [[ $flShowNative ]]; then
- echo "$pckFileFullPath"
- else
- pckFile="${pckFQName//:://}.pm"
- rxPackage="^.*/${pckFile//./\\.}$"
- find $(perl -e 'print join(" ", @INC)') \
- -type f \
- -regextype posix-extended -regex "${rxPackage}" \
- 2>/dev/null | sort | uniq
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement