http://www.perlmonks.org?node_id=177411


in reply to Re: Some suggestions on coding style - a chance to critique
in thread Some suggestions on coding style - a chance to critique

I agree with your style of coding, as it does look a little cleaner and seems that it would prevent the accidental use of global variables. The only concern I have with that is if I were to give the script to someone else with no programming knowledge, they'd have to sort through lines of code to make any necessary changes to variables. When the variables are up top, it's the first thing that they see, making it easier for them to change the variables and less likely for them to screw something up.
  • Comment on Re: Re: Some suggestions on coding style - a chance to critique

Replies are listed 'Best First'.
Re: Re: Re: Some suggestions on coding style - a chance to critique
by lestrrat (Deacon) on Jun 26, 2002 at 17:57 UTC

    True. For a standalone script, it's always nice to see some globally used parameters at the top for easy customization/configuration.

    I do the follwing in such cases...

    If the variables are "configuration" type of variables, I would use constants, not plain scalars. I feel much better in knowing that most ( if not all ) of the globally accessible variables would be visually different from other local variables:

    #!perl use constant LOG_DIRECTORY => '/foo/bar/baz'; use constant SOME_URL => 'http://baboon.com'; main(); sub main { .... some_random_function( LOG_DIRECTORY, $local_variable ); }
      using constant is a very good thing to do. However, if you don't know what constant is doing, you can be very surpsised.
      use constant LOG_DIRECTORY => '/foo/bar/baz'; # Equivalent to sub LOG_DIRECTORY () { '/foo/bar/baz' }
      The most obvious problem is interpolation in double-quoted strings.

      Similarly, you can't use LOG_DIRECTORY as the key to a hash, because it's not a C-style preprocessor declaration, but a function declaration.

      Just, be careful and know what is happening under the hood.

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

        Doh! Nevermind....

        I can see the interpolation problem, but whats this about not using a constant as a hash key?

        #!/usr/bin/perl -w use strict; use constant LOG_DIRECTORY => '/foo/bar/baz'; my %hash; $hash{LOG_DIRECTORY} = 'happy'; print "log directory ${\LOG_DIRECTORY} is $hash{LOG_DIRECTORY}\n"; __END__ produces: log directory /foo/bar/baz is happy

        -Blake

        Try printing out the keys of %hash. You just assigned the value "happy" to the key "LOG_DIRECTORY". To use the constant LOG_DIRECTORY as the key you need to explicitly use it as a function.