perl脚本之目录

来源:

http://www.cnblogs.com/itech/archive/2013/02/20/2919204.html

 

http://stackoverflow.com/questions/5703705/print-current-directory-using-perl?rq=1

 

1)

The following get the script's directory, which is not the same as the current directory. It's not clear which one you want.

1 use Cwd qw( abs_path ); #推荐
2 use File::Basename qw( dirname );  #推荐
3 
4 my $flk = abs_path($0);   #F:/EclipseTest2/a/test1.pl
5 my $flk2 = dirname($flk);  #F:/EclipseTest2/a
6 
7 say $flk2;                 #必须有use v5.10; 才能用say
 

or

1 use Path::Class qw( file );    #我的系统上没有Path::Class模块。需要通过ppm安装一下就有了。本人不推荐用这个,因为产生的都是windows样式的分隔符
2 say file($0)->absolute->dir;   #结果 F:\EclipseTest2\a (windows样式的分隔符)

or

1 use Cwd qw( abs_path );
2 use Path::Class qw( file );
3 say file(abs_path($0))->dir; #结果 F:\EclipseTest2\a(windows样式的分隔符)

 

The middle one handles symlinks different than the other two, I believe. ?

 

2)

To get the current working directory (pwd on many systems), you could use cwd() instead of abs_path:

1 use Cwd qw();
2 my $path =Cwd::cwd();
3 print "$path\n"; #结果 F:/EclipseTest2/a

Or abs_path without an argument:

1 use Cwd qw();
2 my $path =Cwd::abs_path();
3 print "$path\n"; #结果 F:/EclipseTest2/a

See the Cwd docs for details.

 

To get the directory your perl file is in from outside of the directory:

1 use File::Basename qw();
2 my($name, $path, $suffix)=File::Basename::fileparse($0);
3 print "$path\n"; #F:/EclipseTest2/a/ 多一个/    其实$name为test1.pl $suffix为

See the File::Basename docs for more details.

 

3)

You could use FindBin:

1 use FindBin '$RealBin'; #推荐
2 print "$RealBin\n";      #F:/EclipseTest2/a  FindBin sets both $Bin and $RealBin to the current directory.
FindBin is a standard module that is installed when you install Perl. 

转载于:https://www.cnblogs.com/spriteflk/p/5742119.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[2\]中提到了使用execjs时的cwd参数。cwd是execjs.compile()函数的一个参数,用于指定当前工作目录。在这个例子中,cwd被设置为'C:\Users\w001\AppData\Roaming\npm\node_modules'。这个参数的作用是告诉execjs在执行js代码时应该在哪个目录下查找依赖的模块。如果不指定cwd参数,execjs会默认使用当前Python脚本所在的目录作为工作目录。所以,如果你的js代码中引用了其他模块,而这些模块不在当前工作目录下,就需要使用cwd参数来指定正确的路径。 #### 引用[.reference_title] - *1* [execjs随心所欲运行抠出来的js代码:报错什么都不是问题 execjs._exceptions.ProgramError: ReferenceError...](https://blog.csdn.net/panwanpeng/article/details/128048619)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [python使用execjs执行含有document、window等对象的js代码,使用jsdom解决](https://blog.csdn.net/zlc1990628/article/details/116198124)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [爬虫 js逆向,调用js 第三方库报错:execjs._exceptions.ProgramError: ReferenceError: crypto-js is not ...](https://blog.csdn.net/weixin_41354919/article/details/116494736)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值