Windows (XP) / Unix (Solaris) 上的 Perl 脚本配置和相对路径实现

发布于 2024-07-19 03:38:28 字数 1006 浏览 4 评论 0原文

嗨,

我在某种程度上遇到了有关 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雪花飘飘的天空 2024-07-26 03:38:28

问题在于 $config 分配行 -

$config      = '/configuration/config.ini';

由于前导“/”,它会从根目录搜索 config.ini,将路径解释为绝对路径而不是相对路径。 尝试将其更改为

$config      = './configuration/config.ini';

但这仅当您从“MyScript”目录执行 perl 脚本时才有效。 对于这种情况,请查看 FindBin 模块,或者您可以操作 $0 变量来获取 Perl 脚本路径。

The problem lies in the $config assignment line -

$config      = '/configuration/config.ini';

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

$config      = './configuration/config.ini';

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.

浅唱ヾ落雨殇 2024-07-26 03:38:28

这是一个始终知道哪个是您当前目录并使用其他目录的解决方案。

use strict;
use warnings;
use FindBin;
use File::Spec;
use Cwd;
BEGIN {
    $ENV{APP_ROOT} = Cwd::realpath(File::Spec->rel2abs($FindBin::Bin)) ;
}
#now you know your script directory, 
#no matter from where your script is called
#if you have Modules specific for your script which are in 
#a dir "lib" in the same dir as your script is
use lib  (
"$ENV{APP_ROOT}/lib",
);
my $config      = $ENV{APP_ROOT} . '/configuration/config.ini';
#Here is your script
#...
$output_file = "$ENV{APP_ROOT}/$output_dir/output.txt";

所有模块都来自 CORE 发行版,因此您已经安装了它们。
请注意,Windows 接受“/”斜杠,因此您也可以在那里使用它们。

干杯。

Here is a solution to allways know which is your current directory and use your other dirs

use strict;
use warnings;
use FindBin;
use File::Spec;
use Cwd;
BEGIN {
    $ENV{APP_ROOT} = Cwd::realpath(File::Spec->rel2abs($FindBin::Bin)) ;
}
#now you know your script directory, 
#no matter from where your script is called
#if you have Modules specific for your script which are in 
#a dir "lib" in the same dir as your script is
use lib  (
"$ENV{APP_ROOT}/lib",
);
my $config      = $ENV{APP_ROOT} . '/configuration/config.ini';
#Here is your script
#...
$output_file = "$ENV{APP_ROOT}/$output_dir/output.txt";

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文