èªä½pmãuse libãã
ä¸ã«libã¨ãã¤ãã£ã¦ããã«ãããèªä½pmãuseããã¨ããç¸å¯¾ãã¹ã§å¼ã³åºãã¦ãã¾ãã¨å®è¡ãã£ã¬ã¯ããªãéãå ´åï¼cronã§ã¾ããããã¨ãï¼ã«é½åãæªãã£ãããã¾ãã
ããã§ã以ä¸ã®ããã«BEGINãããã¯å ã§ã¹ã¯ãªããèªèº«ã®ãã¹ãã¨ã£ã¦ãã¦ããuse libã¨ããã¦ããã§ããããã®ããæ¹ããããã£ããããªããã ããããããããã£ã¨ããããæ¹ãããã¨ãããã°ãã²ãæãä¸ããã
â main.pl â lib â Hoge â Foo.pm
#!/usr/bin/perl use strict; use warnings; use File::Basename; my $w_dir; BEGIN { $w_dir = dirname $ENV{_} } use lib "$w_dir/lib"; use Hoge::Foo; my $foo = Hoge::Foo->new("tester001"); print $foo->name;
package Hoge::Foo; sub new { my $class = shift; my $name = shift; bless { name => $name } , $class; } sub name { my $self = shift; return $self->{name}; } 1;
追è¨ï¼åæ¥ï¼ï¼
ä¸é¨ãè¨è¿°ã追å ãã¾ããã
追è¨ï¼3/7ï¼ï¼
ä¸è¨ã®ãããªãã¨ãããããããªãã$ENV{_}ãªãã¦ãã®ã使ãããåã«$0ã§ããããããã¨ããã®ãèªç¶ãªæ°ããã¦ã¾ããcronã§ã¾ããã«ãã¦ãããã§ã§ããããã ãã
use File::Basename; my $w_dir; BEGIN { $w_dir = dirname $0 }
ã¿ãããªæãã§ã
以ä¸ãã³ã¡ã³ãã§æãã¦ããã ãããã®ãç´¹ä»ããã¦ããã ãã¾ãã
use File::Basename; use File::Spec; my $w_dir; BEGIN { $w_dir = dirname(File::Spec->rel2abs($0)); }
ä¸è¨ã¨åããããªæãã§ããã常ã«ãã«ãã¹ãåå¾ã§ããããã«ãã¦ãã¾ãã
dirname $0ã ã¨./hoge.plã§å®è¡ããã¨$w_dirã«ã.ããå ¥ãã/home/user/work/hoge.plã§å®è¡ããã¨ã/home/usr/workããå ¥ãã¾ãããä¸è¨ã®ããæ¹ã ã¨å¸¸ã«ã/home/user/workããåå¾ã§ãã¾ããã
ããã²ã¨ã¤ã¯FindBinã¢ã¸ã¥ã¼ã«ã使ãããæ¹ãããã ã¨File::Basenameã使ããã«FindBinã ãã§ããã¾ããã
use FindBin; my $w_dir; BEGIN { $w_dir = $FindBin::Bin; }
追è¨ï¼3/8ï¼ï¼
ããã«FindBin::libsã使ãã°libãæ¢ãã¦ãã¦use libãã¦ããã¾ããè¶ ä¾¿å©ã§ããï¼
use FindBin::libs;
ååã®éããããã©ã«ãã§ã¯libãæ¢ãã«ããã¾ããä»ã®ãã£ã¬ã¯ããªåãæå®ããã°ãããuse libã
use FindBin::libs qw{ base=src print };
ã¨ããã¨use libãããã£ã¬ã¯ããªä¸è¦§ã以ä¸ã®ããã«æ¨æºåºåããã¾ãã
Found */src: /home/webmaster/script/src /var/src
ãçãããã ããæ¹ã
ãã©ãããããã¨ããããã¾ããã