有没有办法通过Perl程序中的命令行开关启用/禁用Smart::Comments?
我想在我的Perl程序中启用/禁用使用Smart::comments模块的注释。我在命令行选项列表中提供了一个--verbose开关,这是我的想法。设置此开关时,我正在考虑启用Smart::Comment模块,如下所示:有没有办法通过Perl程序中的命令行开关启用/禁用Smart::Comments?,perl,perl-module,Perl,Perl Module,我想在我的Perl程序中启用/禁用使用Smart::comments模块的注释。我在命令行选项列表中提供了一个--verbose开关,这是我的想法。设置此开关时,我正在考虑启用Smart::Comment模块,如下所示: #!/usr/bin/perl use Getopt::Long; use Smart::Comments; my $verbose = 0; GetOptions ('verbose' => \$verbose); if (! $verbose) { eval
#!/usr/bin/perl
use Getopt::Long;
use Smart::Comments;
my $verbose = 0;
GetOptions ('verbose' => \$verbose);
if (! $verbose) {
eval "no Smart::Comments";
}
### verbose state: $verbose
然而,这对我不起作用。这似乎与Smart::Comments本身的工作方式有关,因此我怀疑我试图用eval“no…”位禁用模块的方式。有人能给我一些指导吗?从脚本中取出
使用智能::注释
行,然后运行脚本,使用或不使用-MSmart::注释
选项。使用-M
选项就像将use
语句放在脚本的开头
# Smart comments off
$ perl my_script.pl
# Smart comments on
$ perl -MSmart::Comments my_script.pl ...
另请参见
Smart::Comments
文档中的变量。
在这里,您可以在脚本中使用Smart::Comments
,如
use Smart::Comments -ENV;
然后跑
$ perl my_script.pl
$ Smart_Comments=0 perl my_script.pl
在没有智能注释的情况下运行,以及
$ Smart_Comments=1 perl my_script.pl
使用智能注释运行
更新智能::注释模块是一个源过滤器。在运行时尝试打开和关闭它(例如,
eval“no Smart::Comments”
)将不起作用。充其量,您可以在编译时进行一些配置(例如,在加载Smart::Comments
之前,在BEGIN{}
块中):
使用“如果”杂注:
如果不需要,这不会加载Smart::Comments。是的,我宁愿对脚本用户隐藏它。此外,我没有使用perl运行此脚本,我正在使脚本可执行并直接运行它。谢谢,这使Smart::Comments在运行时不能通过eval块打开/关闭变得更有意义。我试图找出如何利用BEGIN块,然后空出来,这是我见过的获得我想要的东西的最好方法!这并不是因为它是一个
no Smart::Comments的源过滤器代码>不起作用。它可以查找no Smart::Comments代码>并在未更改的情况下传递代码,直到遇到use Smart::Comments代码>。例如,是一个关键字插件(类似于源过滤器),它使用“已加载但未激活”这一概念从词汇上界定其效果。@ikegami-我不理解你的评论。如果在编译时遇到no Smart::Comments
语句,则该语句与您描述的完全相同。在运行时,eval“no Smart::Comments”
的范围仅限于eval
语句——它只能在eval语句中禁用智能注释。也许这仍然“有效”;这不是很有用。
use strict;
use warnings;
BEGIN { $ENV{Smart_Comments} = " @ARGV " =~ / --verbose / }
use Smart::Comments -ENV;
...
use if !$ENV{MY_APP_NDEBUG}, 'Smart::Comments';
# or
use if $ENV{MY_APP_DEBUG}, 'Smart::Comments';