在Perl文件中包含PHP配置值
我在CodeIgniter(v2.x)中有一个PHP应用程序,它有一个配置文件,它的值采用相当标准的PHP格式,例如:在Perl文件中包含PHP配置值,php,codeigniter,perl,Php,Codeigniter,Perl,我在CodeIgniter(v2.x)中有一个PHP应用程序,它有一个配置文件,它的值采用相当标准的PHP格式,例如: $config['key1'] = 'value1'; $config['key2'] = 'value2'; 我正在编写一个Perl程序,每晚运行一次,将信息解析到数据库中供PHP使用/显示,我可以让Perl程序从PHP读取配置值吗 我尝试了PHP::Include module,但是生成的Perl值似乎是空的(使用DEBUG)模块。我还尝试了Config::ini文件模块
$config['key1'] = 'value1';
$config['key2'] = 'value2';
我正在编写一个Perl程序,每晚运行一次,将信息解析到数据库中供PHP使用/显示,我可以让Perl程序从PHP读取配置值吗
我尝试了PHP::Include module
,但是生成的Perl值似乎是空的(使用DEBUG)模块。我还尝试了Config::ini文件模块
,但我的源代码未配置为ini
文件
谢谢是的,您可以将配置数组转换为与语言无关的格式,如json:
$json = json_encode($this->config->config);
现在您已经有了json格式的配置数组,您可以将其保存到一个文件中,或者直接从程序中通过ajax请求检索它,或者对它执行任何操作。如果它是这样一个简单的配置文件
<?php
/**
*
* service: /www/vhosts/x456/docs/config.php
* program: community one
* version: 1.01, 01/01/2009 01:01:01
*
**/
$config = array ();
$config['system_admin'] = 1;
$config['system_name'] = 'Community One';
$config['session_name'] = 'community';
$config['system_email'] = '@gmail.com';
$config['system_allow'] = '127.0.0.1';
$config['storage_type'] = 'file';
$config['host_name'] = '';
$config['service_path'] = '/';
$config['system_root'] = 'c:/services/www/vhosts/x456/';
$config['document_root'] = 'c:/services/www/vhosts/x456/docs/';
$config['database_name'] = 'community';
$config['database_host'] = 'localhost';
$config['database_user'] = 'admin';
$config['database_pass'] = '';
$config['database_type'] = 'mysqli';
$config['database_port'] = '3306';
$config['database_salt'] = '*^&$_';
$config['min_timeout'] = 180;
$config['max_timeout'] = 31557600;
$config['ssl_only'] = FALSE;
?>
如果需要使用多种语言,请考虑将配置更改为更独立于语言的格式。XML、JSON、YAML、INI等。。或者编写一个适配器PHP程序,Perl可以调用该程序,它读取配置并以Perl程序可以使用的方式显示配置。或者,如果配置足够简单,试着用Perl编写自己的解析器。如果格式真的“相当标准”,那么一个简单的正则表达式就可以对这些设置进行grep处理。@stickybit没有更多的内容可以添加到您的解释中,我在回答中将整个配置数组转换为json格式。@mario-XML、json和YAML都是“相当标准”的配置格式不能被正则表达式解析,更不用说简单的正则表达式了。
use strict;
use warnings;
use Data::Dumper qw ( Dumper );
#error log location and name
my $log = 'C:/services/www/log/errors.txt';
# the hash container
my %data;
# the config file processor
sub configFile
{
# grab the config files location
my $file = shift;
# open the config file up
open ( CONFIG, '<', $file ) or logError ( 0, $file );
# read the config file, line by line
while ( my $line = <CONFIG> )
{
# lose the eol and any spaces found >> (^|$)
$line = trim ( $line );
# move to next line if we don't have a $var
next if $line !~ /^\$/;
# move to the next line if there is no
# (key = value) pair
my $find = index $line, '=';
# grab the array & key name(s) + clean them up
my $name = rtrim ( substr $line, 0, $find );
# grab the value + clean it up
my $value = ltrim ( substr $line, ( $find + 1 ) );
# final check, skip over $var(s) = array(), []
# process only $var['key'] = values!
next if ( my $pos = index $name, '[' ) == -1;
# set the (hash key) name = $(hash key)
my $hash = substr $name, 1, ( $pos - 1 );
# set the variables (key) name = $(hash key)[(key)]
my $key = substr $name, ( $pos + 2 ), -2;
# strip ('|'|;) from the variable(s) value
if ( $value =~ m/^'|^"/ )
{
$value = substr $value, 1, -2;
}
else
{
$value = substr $value, 0, -1;
}
# add the varaible to the data hash
$data{$hash}{$key} = $value;
}
# done, close it
close CONFIG;
}
sub logError ( )
{
my ( $type, $data ) = @_;
open ( ERRORS, '>>', $log );
print ERRORS $type . ", " . $data . "\n";
close ERRORS;
exit ( 0 );
}
sub ltrim
{
my $s = shift;
$s =~ s/^\s+//;
return $s
};
sub rtrim
{
my $s = shift;
$s =~ s/\s+$//;
return $s
};
sub trim
{
my $s = shift;
$s =~ s/^\s+|\s+$//g;
return $s
};
# the php config style file to process
configFile ( 'C:\\services\\www\\config.php' );
# just print the $data hash out so you
# can see what it returns
print Dumper \%data;
$VAR1 = {
'config' => {
'ssl_only' => 'FALSE',
'database_salt' => '*^&$_',
'system_allow' => '127.0.0.1',
'system_email' => '@gmail.com',
'max_timeout' => '31557600',
'database_type' => 'mysqli',
'database_pass' => '',
'storage_type' => 'file',
'database_user' => 'admin',
'min_timeout' => '180',
'system_admin' => '1',
'system_name' => 'Community One',
'system_root' => 'c:/services/www/vhosts/x456/',
'database_host' => 'localhost',
'database_port' => '3306',
'session_name' => 'community',
'service_path' => '/',
'database_name' => 'community',
'host_name' => '',
'document_root' => 'c:/services/www/vhosts/x456/docs/'
}
};