有没有办法通过我的Perl程序中的命令行开关启用/禁用Smart :: Co
发布时间:2020-12-15 23:23:42 所属栏目:大数据 来源:网络整理
导读:我想在我的Perl程序中启用/禁用使用模块Smart :: Comments的注释.我通过提供–verbose开关作为命令行选项列表的一部分,玩弄了这样做的想法.当设置此开关时,我正在考虑启用Smart :: Comment模块,如下所示: #!/usr/bin/perluse Getopt::Long;use Smart::Comme
我想在我的Perl程序中启用/禁用使用模块Smart :: Comments的注释.我通过提供–verbose开关作为命令行选项列表的一部分,玩弄了这样做的想法.当设置此开关时,我正在考虑启用Smart :: Comment模块,如下所示:
#!/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 …”位来禁用模块的方式.有人可以给我一些指导吗? 解决方法
从脚本中取出使用Smart :: Comments行,并使用或不使用-MSmart :: Comments选项运行脚本.使用-M<模块>选项就像使用< module>脚本开头的语句.
# Smart comments off $perl my_script.pl # Smart comments on $perl -MSmart::Comments my_script.pl ... 另请参阅Smart :: Comments文档中的 use Smart::Comments -ENV; 然后跑 $perl my_script.pl $Smart_Comments=0 perl my_script.pl 没有聪明的评论,并且 $Smart_Comments=1 perl my_script.pl 运行聪明的评论. 更新Smart :: Comments模块是一个源过滤器.试图在运行时打开和关闭它(例如,eval“no Smart :: Comments”)将不起作用.充其量,您可以在编译时(例如,在BEGIN {}块中进行一些配置,然后再加载Smart :: Comments): use strict; use warnings; BEGIN { $ENV{Smart_Comments} = " @ARGV " =~ / --verbose / } use Smart::Comments -ENV; ... (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |