How do you use a variable in lib Path?

后端 未结 3 1870
离开以前
离开以前 2021-02-19 14:43

I would like to use the $var variable in lib path.

my $var = \"/home/usr/bibfile;\"

use lib \"$var/lib/\";

However when I do th

3条回答
  •  说谎
    说谎 (楼主)
    2021-02-19 15:28

    Variables work fine in use lib, well, just like they do in any string. However, since all use directives are executed in BEGIN block, your variable will be not yet initialized at the moment you run use, so you need to put initialization in BEGIN block too.

    my $var;
    BEGIN { $var = "/home/usr/bibfile"; }
    use lib "$var/lib/";
    
    use Data::Dumper;
    print Dumper \@INC;
    

    Gives:

    $VAR1 = [
          '/home/usr/bibfile/lib/',
          # ... more ...
        ];
    

提交回复
热议问题