Perl:常数&;要求
我有一个带有常量的配置文件(config.pl):Perl:常数&;要求,perl,include,constants,require,Perl,Include,Constants,Require,我有一个带有常量的配置文件(config.pl): #!/usr/bin/perl use strict; use warnings; use Net::Domain qw(hostname hostfqdn hostdomain domainname); use constant URL => "http://".domainname()."/"; use constant CGIBIN => URL."cgi-bin/"; use constant CSS => URL.
#!/usr/bin/perl
use strict;
use warnings;
use Net::Domain qw(hostname hostfqdn hostdomain domainname);
use constant URL => "http://".domainname()."/";
use constant CGIBIN => URL."cgi-bin/";
use constant CSS => URL."html/css/";
use constant RESSOURCES => URL."html/ressources/";
...
我想在index.pl中使用这些常量,因此index.pl以:
#!/usr/bin/perl -w
use strict;
use CGI;
require "config.pl";
如何使用URL,CGI。。。在index.pl中?谢谢,
再见
编辑
我找到了一个解决方案:
config.pm
#!/usr/bin/perl
package Config;
use strict;
use warnings;
use Net::Domain qw(hostname hostfqdn hostdomain domainname);
use constant URL => "http://".domainname()."/";
use constant CGIBIN => URL."cgi-bin/";
1;
index.pl
BEGIN {
require "config.pm";
}
print Config::URL;
结束
或
并且必须在所需源的末尾返回一个真值。通常:
1;
虽然,在某些模块上,我已经完成了:
print "My::Mod included...\n";
作为文件中的最后一条语句。无论何时打印单个字符,print
都会返回true
看
故障排除
- 这可能是一个目录问题。
文件必须位于.pl
中,或由文件路径修改李>@INC
perl -Mconfig.pl -e 1
如果失败,请查看错误消息。事实上,在任何情况下,使用strict和warnings都应该比“Oops,it failed”得到更多。当您使用strict时,您不能使用裸词 裸字本质上是一个变量名,没有符号(
$,@,%,&,*
)
我建议看一下CPAN上创建常量的模块
如果你不小心的话,
use constant
pragma有一堆神秘的魔法,可能导致代码调试困难。你想在这里做的是设置一个可以导出的Perl模块
将以下内容放入“MyConfig.pm”中:
#!/usr/bin/perl
package MyConfig;
use strict;
use warnings;
use Net::Domain qw(hostname hostfqdn hostdomain domainname);
use constant URL => "http://".domainname()."/";
use constant CGIBIN => URL."cgi-bin/";
use constant CSS => URL."html/css/";
use constant RESSOURCES => URL."html/ressources/";
require Exporter;
our @ISA = 'Exporter';
our @EXPORT = qw(hostname hostfqdn hostdomain domainname URL CGIBIN CSS RESSOURCES);
然后使用它:
use MyConfig; # which means BEGIN {require 'MyConfig.pm'; MyConfig->import}
通过在
MyConfig
包中将@ISA
设置为Exporter
,可以将包设置为从Exporter
继承Exporter
提供由use MyConfig隐式调用的import
方法代码>行。变量@EXPORT
包含默认情况下应导入的Exporter
名称列表。Perl的文档中还有许多其他选项,在的文档中,它不适合我,不需要在config.pl中导出,在index.pl中导入,它们实际上适合我。您收到的是什么类型的错误消息?常量
变量实际上是子例程。如果没有package
声明,它们必须位于main::
命名空间中。在/var/www/cgi-bin/index.pl第35行使用“strict subs”时,不允许使用空字“URL”。由于编译错误,/var/www/cgi-bin/index.pl的执行被中止。如果有一个名为cgi的常量和一个名为cgi的包,您就会遇到麻烦。。。许多混乱将接踵而至!说得好!现在就改变它,常量
pragma实际上没有什么神秘或神奇的地方。它纯粹是创建常量子例程的语法糖<代码>使用常数X=>5
与subx(){5}完全相同。事实上,我更喜欢后者,因为它是一个提醒,而不是常数实际上是子例程。也许不是“魔法”,但肯定是神秘的,因为你更喜欢后者的原因:糖试图使子例程看起来像一个变量,但在你期望的地方,它的行为不像一个变量(如插入字符串内)。回答得很好!非常感谢您的解释。
perl -Mconfig.pl -e 1
#!/usr/bin/perl
package MyConfig;
use strict;
use warnings;
use Net::Domain qw(hostname hostfqdn hostdomain domainname);
use constant URL => "http://".domainname()."/";
use constant CGIBIN => URL."cgi-bin/";
use constant CSS => URL."html/css/";
use constant RESSOURCES => URL."html/ressources/";
require Exporter;
our @ISA = 'Exporter';
our @EXPORT = qw(hostname hostfqdn hostdomain domainname URL CGIBIN CSS RESSOURCES);
use MyConfig; # which means BEGIN {require 'MyConfig.pm'; MyConfig->import}