in reply to change 'use constant' value through commandline arguments?

It isn't perfect, but:     use constant DEBUGGING => grep $_ eq "-d", @ARGV; otherwise you'd need to do all of the argument processing inside a BEGIN block:

my %options; BEGIN { # process @ARGV, putting results in %options } use constant DEBUGGING => $options{debug};

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
Re: (tye)Re: change 'use constant' value through commandline arguments?
by neuroball (Pilgrim) on Mar 14, 2002 at 20:19 UTC
    Here is what I did come up with through the education/help received in this thread:

    # set debugging switch to OFF my $debug_switch = 0; # go through the argument evaluation at compile time BEGIN { # check each argument foreach $_ (@ARGV) { # is the debug switch submitted? if (/^-d$/i) { # yup. set debugging switch to ON $debug_switch = 1; } } } # set DEBUGGING to the debug_switch value use constant DEBUGGING => $debug_switch;

    It works, doesn't throw any warnings, and I use a constant like it is normally done: Define once, use x times.

      Rather than just setting it to 1, it may be more flexible to use $debug_switch++ instead; that way you could increase the debug level with every -d on the command-line, letting you do things like...

      use constant VAGUE => 0; use constant DETAILED => 1; use constant PEDANTIC => 2; if ( @x ) { warn( "\@x is set\n" ) if DEBUG > VAGUE; warn( scalar(@x)," elements\n" ) if DEBUG > DETAILED; { local $" = "\n "; warn( "They are...\n @x\n" ) if DEBUG > PEDANTIC; } }

      Lame example, but hopefully you get the point... :)

          --k.


      tye's original suggestion can easilly be modified to use 1/0 instead of -d/undef for TRUE/FALSE values.... is that why you opted for his second (longer) suggestion?
      use constant DEBUGGING => (grep $_ eq "-d", @ARGV) ? 1 : 0;

      -Blake

      Would not it be easier just to code
      use Getopt::Long;
      GetOptions("debug" => \$debug_switch);
      
      or even
      use Getopt::Long;
      GetOptions("debug+" => \$debug_switch);
      
      which allows several levels of debugging.
        It might be easier... but it is shorter.

        Btw. In my 'solution' above I made a mistake that I just found as I was porting the script from my IDE (Komodo) to the server.
        The undefined constant (debugging turned off) throws warnings about void constants. To get around this I used the following short fix:
        # set DEBUGGING to the debug_switch value use constant DEBUGGING => $debug_switch || 0;
        Now to answer some questions...
        Q: 'Why did I opt for the long version?'
        A: It is clear and maintainable, even by persons with a minimum perl knowledge. I do this just to be nice to the people who have to live with my code.

        Q: 'Why not use more than one level of debugging messages?'
        A: I didn't need more than one. And knowing the application I doubt very much that this will change.

        But... if I ever in need to use more than on level of debugging I will sure come back to this thread and look at the offered solutions for this feature.

        thx /oliver