perl php 配置文件 INCLUDE require WEB里面经常用到配置文件,例如存放数据库访问资料等。 PHP倒是比较简单,写入一个 PHP 文件,例如 <?php www.shengfang.org $config['mode']="Configure"; ?> $fp = fopen("dbconfig.php", "w+"); fwrite($fp,"<?php \n"); foreach ($config as $name => $value) { www.shengfang.org # fwrite($fp,"$name=$value\n"); fwrite($fp,"\$config['$name']"."=\"$value\";\n"); } fwrite($fp,"?> \n"); fclose( $fp ); require "./dbconfig.php"; global $config; www.shengfang.org echo $config['mode']; PERL就比较讨厌,看了一下 require函数 www.shengfang.org 用require函数可以把程序分割成多个文件并创建函数库。例如,在myfile.pl中有定义好的Perl函数,可用语句require ("myfile.pl"); 在程序中包含进来。当Perl解释器看到这一语句,就在内置数组变量 at INC指定的目录中寻找文件myfile dot pl。如果找到了,该文件中的语句就被执行,否则程序终止并输出错误信息: unshift ( at INC, "/u/perldir"); require ("mysub dot pl"); 对unshift的调用把目录/u/perldir添加到 at INC数组,对require的调用将mysub dot pl文件的内容包含进来作为程序的一部分。 注意: 1、应该使用unshift来向 at INC中添加目录,而不是push。因为push增加到@INC的末尾,则该目录将被最后搜寻。 2、如果你的库文件名与/usr/local/lib/perl中的某文件同名,则不会被包含进来,因为require只包含同名文件中的第一个。 试验一下,定义一个PERL #!/usr/bin/perl www.shengfang.org $sfsf="sdfsdf"; require "./sfinc.inc" print $sfsf."\n"; www.shengfang.org 结果:syntax error at ./sftest.pl line 21, near "print" Execution of ./sftest.pl aborted due to compilation errors. 改为 sub mm() { $sfsf="sdfsdf"; } &mm(); www.shengfang.org 结果 ./sfinc.inc did not return a true value at ./sftest.pl line 19. 最后改为 sub mm() www.shengfang.org { $sfsf="sdfsdf"; }1 require "./sfinc.inc" &mm(); www.shengfang.org print $sfsf."\n"; 在PERL中被require()的.cgi文件为什么要在最后一行加个1? 这是 perl 规定的! 目的是返回真值。 www.shengfang.org 字体:大 中 小 |