Using a DEBUG constant and assigning its value via an environment variable are
both common, but has a drawback and would not be my choice of approach. I
prefer to use a lexical var (with file scope) and assign it via command line
option.
Let's assume you have multiple scripts using the DEBUG constant where some (or
possibly all) are run from cron jobs. If you set the debug mode based on an
environment variable, then that could/would affect the running of all of the
scripts, which you may or may not want. If you set the debug mode based on
command line option(s), then you could be much more selective not only one
which script(s) have debug enabled, but also (optionally) the level of
verbosity.
#!/usr/bin/perl
use 5.010;
use warnings;
use strict;
use Getopt::Long;
my $DEBUG = 0;
my $VERBOSE = 0;
GetOptions(
'D' => \$DEBUG,
'v=i' => \$VERBOSE
);
if( $DEBUG ){
say 'hello';
say 'goodbye' if $VERBOSE > 1;
}
Ron
From: "Sharanbasappa Raghapur, ERS, HCLTech" <[email protected]>
To: "Perl Beginners" <[email protected]>
Sent: Tuesday, July 7, 2015 4:40:35 AM
Subject: Debugging and passing constant value at run time
Hi,
I am using constant mainly to enable printing of debugging messages (e.g. use
constant DEBUGGING_L1 => 0;)
Normally, the value is ‘0’ and when I need to see debug messages, I make this 1
and run the script.
Is there a way to allocate value to constants at run time so that I can avoid
editing the script again and again?
Also, is there a better way to handle debugging?
Regards,
Sharan
::DISCLAIMER::
----------------------------------------------------------------------------------------------------------------------------------------------------
The contents of this e-mail and any attachment(s) are confidential and intended
for the named recipient(s) only.
E-mail transmission is not guaranteed to be secure or error-free as information
could be intercepted, corrupted,
lost, destroyed, arrive late or incomplete, or may contain viruses in
transmission. The e mail and its contents
(with or without referred errors) shall therefore not attach any liability on
the originator or HCL or its affiliates.
Views or opinions, if any, presented in this email are solely those of the
author and may not necessarily reflect the
views or opinions of HCL or its affiliates. Any form of reproduction,
dissemination, copying, disclosure, modification,
distribution and / or publication of this message without the prior written
consent of authorized representative of
HCL is strictly prohibited. If you have received this email in error please
delete it and notify the sender immediately.
Before opening any email and/or attachments, please check them for viruses and
other defects.
----------------------------------------------------------------------------------------------------------------------------------------------------