How do I use a Perl module from a relative location?

前端 未结 8 2198
予麋鹿
予麋鹿 2020-12-23 16:41

I have a dir called foo, and in that I have lib and bin. The scripts in bin need stuff in lib. I do someth

8条回答
  •  余生分开走
    2020-12-23 17:17

    Just to add my own two cents to this collection of answers, I usually solve this problem using something along these lines:

    use lib do {
        use Cwd 'realpath';
        my ($dir) = __FILE__ =~ m{^(.*)/};  # $dir = path of current file
        realpath("$dir/../lib");            # path of '../lib' relative to $dir
    };
    

    I like using a do block because everything needed is so neatly contained therein. If you ever need to copy/paste, or try to understand your code at a later time you don't have to look for a separate BEGIN block or anything like that.

    The above code naïvely assumes that / is used as a dir/filename separator.

提交回复
热议问题