Windows (XP) / Unix (Solaris) 上的 Perl 脚本配置和相对路径实现
嗨,
我在某种程度上遇到了有关 perl 脚本开发的难题。 我使用标准(基本)Perl 安装编写了一个小型 Perl 脚本。 我有以下设置:
C:\MyScript\perl.pl
C:\MyScript\configuration\config.ini
C:\MyScript\output\output.txt
这是 perl.pl 源:
$config = '/configuration/config.ini';
$conf = Config::IniFiles->new( -file => $config_file );
$output_dir = conf->val('output', 'out_dir');
$output_file = "$output_dir/output.txt";
open (out, ">$output_file") || die ("It's not your day mate!");
print out "This is a test...";
close out;
这是 config.ini 内容:
[output]
output_dir = C:\MyScript\output
我遇到的问题是第二行 ($conf) 似乎无法打开该文件地点。 由于我将在 Windows 和 Unix 环境中使用这个脚本(不安装任何附加模块),我想知道如何解决这个问题? 我希望创建一个可以通过 config.ini 文件完全配置的脚本。 然而,该配置仅在我给出绝对路径时才有效,如下所示:
$config = 'C:\MyScript\configuration\config.ini';
但由于这将部署到多个不同的环境,因此不可能修改脚本源。 你们会推荐什么? 面对这样的场景,人们应该如何应对?
非常感谢任何帮助和/或建议。
一切顺利, MC
Heylo,
I'm experiencing, somewhat of, a conundrum regarding perl script development. I have written a small Perl script using the standard (basic) Perl installation. I have the following setup:
C:\MyScript\perl.pl
C:\MyScript\configuration\config.ini
C:\MyScript\output\output.txt
This is the perl.pl source:
$config = '/configuration/config.ini';
$conf = Config::IniFiles->new( -file => $config_file );
$output_dir = conf->val('output', 'out_dir');
$output_file = "$output_dir/output.txt";
open (out, ">$output_file") || die ("It's not your day mate!");
print out "This is a test...";
close out;
This is the config.ini contents:
[output]
output_dir = C:\MyScript\output
The problem I am having is that the second line ($conf) appears to be having trouble opening the file at that location. As I will be using this script in both a windows and unix environment (without installing any addition modules) I was wondering how I could get around this? What I was hoping was to create a script that would entirely configurable through the config.ini file. The config however, only works if I give it's absolute path, like so:
$config = 'C:\MyScript\configuration\config.ini';
But since this will be deployed to several diverse environments modifying the scripts source is out of the question. What would you guys recommend? How should one approach such a scenario?
Any help and/or advice is greatly appreciated.
All the best,
MC
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题在于 $config 分配行 -
由于前导“/”,它会从根目录搜索 config.ini,将路径解释为绝对路径而不是相对路径。 尝试将其更改为
但这仅当您从“MyScript”目录执行 perl 脚本时才有效。 对于这种情况,请查看 FindBin 模块,或者您可以操作 $0 变量来获取 Perl 脚本路径。
The problem lies in the $config assignment line -
This searches for config.ini from the root directory due to the leading '/', interpreting the path as an absolute rather than relative. Try changing it to
This though will only work if you execute the perl script from the 'MyScript' directory. Have a look at the FindBin module for such cases or you could manipulate the $0 variable to get your perl script path.
这是一个始终知道哪个是您当前目录并使用其他目录的解决方案。
所有模块都来自 CORE 发行版,因此您已经安装了它们。
请注意,Windows 接受“/”斜杠,因此您也可以在那里使用它们。
干杯。
Here is a solution to allways know which is your current directory and use your other dirs
All Modules are from the CORE distribution so you have them installed.
Note that Windows accepts "/" slashes, so you can use them there too.
Cheers.