perl配置文件里面包含变量?
perl configuration file which contains variables?
我想创建一个 perl 配置文件。我想要一个有变量的文件格式。所以像这样:
DefaultDirectory = /var/myProgram/
OutputDirectory = $DefaultDirectory/output
InputDirectory = $DefaultDirectory/input
这看起来很简单,但我不确定 perl 有什么可用的。我看到的 perl ini 选项似乎不支持它。我查看了 YAML,但它似乎有点矫枉过正。
谁能推荐一个好的文件格式和支持它的支持简单变量的CPAN模块?我坚持使用 perl 5.5,所以希望是一个较旧的模块。
您是否考虑过编写自己的包含配置的 perl 模块?
类似于 (MyConfig.pm
):
package MyConfig;
our $DefaultDirectory = '/path/to/somewhere';
our $setting_for_something = 5;
1;
然后您可以使用 use MyConfig;
导入它。您可能需要设置 use lib
或 FindBin
以首先找到模块(取决于您调用脚本的位置 - use
将搜索 cwd)。
但真的 - perl 5.5
?那是......非常值得更新,因为这是 2004 年的版本。我希望你不要 运行 在 10 年前的软件上做太多的事情 - 在这期间世界已经改变了很多。 (Perl 也是如此)
test.cfg
# Simple variables
DefaultDirectory = /var/myProgram
OutputDirectory = $DefaultDirectory/output
InputDirectory = $DefaultDirectory/input
# Blocks of related variables
<host_dev>
host = devsite.example.com
user = devuser
password = ComeOnIn
</host_dev>
<host_prod>
host = prodsite.example.com
user = produser
password = LockedDown
</host_prod>
test.pl
#!/usr/bin/perl
use strict;
use warnings;
use Config::General;
my $conf = Config::General->new(
-ConfigFile => 'test.cfg',
-InterPolateVars => 1
);
my %config = $conf->getall;
print <<HERE;
Default directory: $config{'DefaultDirectory'}
Output directory: $config{'OutputDirectory'}
Input directory: $config{'InputDirectory'}
Development host: $config{'host_dev'}{'host'}
Development password: $config{'host_dev'}{'password'}
Production host: $config{'host_prod'}{'host'}
Production password: $config{'host_prod'}{'password'}
HERE
输出:
Default directory: /var/myProgram
Output directory: /var/myProgram/output
Input directory: /var/myProgram/input
Development host: devsite.example.com
Development password: ComeOnIn
Production host: prodsite.example.com
Production password: LockedDown
我想创建一个 perl 配置文件。我想要一个有变量的文件格式。所以像这样:
DefaultDirectory = /var/myProgram/
OutputDirectory = $DefaultDirectory/output
InputDirectory = $DefaultDirectory/input
这看起来很简单,但我不确定 perl 有什么可用的。我看到的 perl ini 选项似乎不支持它。我查看了 YAML,但它似乎有点矫枉过正。
谁能推荐一个好的文件格式和支持它的支持简单变量的CPAN模块?我坚持使用 perl 5.5,所以希望是一个较旧的模块。
您是否考虑过编写自己的包含配置的 perl 模块?
类似于 (MyConfig.pm
):
package MyConfig;
our $DefaultDirectory = '/path/to/somewhere';
our $setting_for_something = 5;
1;
然后您可以使用 use MyConfig;
导入它。您可能需要设置 use lib
或 FindBin
以首先找到模块(取决于您调用脚本的位置 - use
将搜索 cwd)。
但真的 - perl 5.5
?那是......非常值得更新,因为这是 2004 年的版本。我希望你不要 运行 在 10 年前的软件上做太多的事情 - 在这期间世界已经改变了很多。 (Perl 也是如此)
test.cfg
# Simple variables
DefaultDirectory = /var/myProgram
OutputDirectory = $DefaultDirectory/output
InputDirectory = $DefaultDirectory/input
# Blocks of related variables
<host_dev>
host = devsite.example.com
user = devuser
password = ComeOnIn
</host_dev>
<host_prod>
host = prodsite.example.com
user = produser
password = LockedDown
</host_prod>
test.pl
#!/usr/bin/perl
use strict;
use warnings;
use Config::General;
my $conf = Config::General->new(
-ConfigFile => 'test.cfg',
-InterPolateVars => 1
);
my %config = $conf->getall;
print <<HERE;
Default directory: $config{'DefaultDirectory'}
Output directory: $config{'OutputDirectory'}
Input directory: $config{'InputDirectory'}
Development host: $config{'host_dev'}{'host'}
Development password: $config{'host_dev'}{'password'}
Production host: $config{'host_prod'}{'host'}
Production password: $config{'host_prod'}{'password'}
HERE
输出:
Default directory: /var/myProgram
Output directory: /var/myProgram/output
Input directory: /var/myProgram/input
Development host: devsite.example.com
Development password: ComeOnIn
Production host: prodsite.example.com
Production password: LockedDown