gpt4 book ai didi

perl - 有没有办法通过我的 Perl 程序中的命令行开关启用/禁用 Smart::Comments?

转载 作者:行者123 更新时间:2023-12-04 22:50:13 25 4
gpt4 key购买 nike

我想在我的 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 ..."位禁用模块的方式。谁能给我一些指导?

最佳答案

拿出use Smart::Comments行出脚本,并在有或没有 -MSmart::Comments 的情况下运行你的脚本选项。使用 -M<module>选项就像放一个 use <module>脚本开头的语句。

# Smart comments off
$ perl my_script.pl

# Smart comments on
$ perl -MSmart::Comments my_script.pl ...

另见 $ENV{Smart_Comments} 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

运行智能评论。

更新 Smart::Comments模块是一个源过滤器。尝试在运行时打开和关闭它(例如 eval "no Smart::Comments" )将不起作用。充其量,您可以在编译时进行一些配置(例如,在 BEGIN{} block 中,在加载 Smart::Comments 之前):
use strict;
use warnings;
BEGIN { $ENV{Smart_Comments} = " @ARGV " =~ / --verbose / }
use Smart::Comments -ENV;
...

关于perl - 有没有办法通过我的 Perl 程序中的命令行开关启用/禁用 Smart::Comments?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7985766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com