Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl:常数&;要求_Perl_Include_Constants_Require - Fatal编程技术网

Perl:常数&;要求

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.

我有一个带有常量的配置文件(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."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}