简体   繁体   English

Perl动态包含文件

[英]Perl include file dynamically

I have a doubt in perl , I need to get filename in command line arguments/switches/options and based on that i need to include/require/use that file in my script. 我在perl中有一个疑问,我需要在命令行参数/开关/选项中获取文件名,因此我需要在脚本中包含/要求/使用该文件。 So by using that getopt variable am able to get it done. 因此,通过使用该getopt变量能够完成它。 However if i have used "use strict" in my script i could not get it. 但是,如果我在脚本中使用了“严格使用”,则无法获取。 Please help on this 请帮忙

I tried as follows and its working 我尝试了如下及其工作

base.pm: base.pm:

our $def_name = "me";

main.pl: main.pl:

my $name = "mathlib.pm";
require $name;
print $def_name;

When i include the follwoing lines in the main.pl and base.pm, 当我在main.pl和base.pm中包含以下行时,

use strict;
use warnings;

I am getting error, 我遇到错误,

Global symbol "$def_name" requires explicit package name .
Execution of main.pl aborted due to compilation errors.

$def_name hasn't been exported from mathlib.pm , it's a package variable not a global. $def_name尚未从mathlib.pm导出,它是一个包变量,而不是全局变量。 $mathlib::def_name will work, or you can use the Exporter module to export it into the namespace of any module which uses it. $mathlib::def_name将起作用,或者您可以使用Exporter模块将其导出到使用该模块的任何模块的名称空间中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM