Is there a way to enable / disable Smart :: Comments using the command line in my Perl program?

I would like to enable / disable comments in my Perl program that use the Smart :: Comments module. I played around with the idea of ​​doing this by providing the -verbose switch as part of my list of command line options. When this switch is set, I thought about turning on the Smart :: Comment module as follows:

#!/usr/bin/perl use Getopt::Long; use Smart::Comments; my $verbose = 0; GetOptions ('verbose' => \$verbose); if (! $verbose) { eval "no Smart::Comments"; } ### verbose state: $verbose 

However, this does not work for me. There seems to be something with how Smart :: Comments works, so I am suspicious of how I try to disable the module with the eval bit "no ...". Can someone offer me some recommendations on this?

+4
source share
2 answers

Remove the use Smart::Comments from the script and run you script with or without the -MSmart::Comments option. Using the -M<module> parameter is like putting the use <module> statement at the top of your script.

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

Also see the $ENV{Smart_Comments} variable in Smart::Comments docs. Here you should use Smart::Comments in the script as

 use Smart::Comments -ENV; 

and then run

 $ perl my_script.pl $ Smart_Comments=0 perl my_script.pl 

to run without intelligent comments and

 $ Smart_Comments=1 perl my_script.pl 

to run with smart comments.


Update Smart::Comments module is an initial filter. Attempting to turn it on and off at run time (for example, eval "no Smart::Comments" ) will not work. At best, you can do some configuration at compile time (say, in the BEGIN{} block BEGIN{} before loading Smart::Comments ):

 use strict; use warnings; BEGIN { $ENV{Smart_Comments} = " @ARGV " =~ / --verbose / } use Smart::Comments -ENV; ... 
+10
source

Use the "if" pragma:

 use if !$ENV{MY_APP_NDEBUG}, 'Smart::Comments'; # or use if $ENV{MY_APP_DEBUG}, 'Smart::Comments'; 

It does not load Smart :: Comments if it is not required.

0
source

Source: https://habr.com/ru/post/1379362/


All Articles