Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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

by lestrrat (Deacon)
on Jun 26, 2002 at 17:57 UTC ( #177457=note: print w/replies, xml ) Need Help??

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

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 => ''; main(); sub main { .... some_random_function( LOG_DIRECTORY, $local_variable ); }
  • Comment on Re: Re: Re: Some suggestions on coding style - a chance to critique
  • Download Code

Replies are listed 'Best First'.
Re4: Some suggestions on coding style - a chance to critique
by dragonchild (Archbishop) on Jul 01, 2002 at 18:55 UTC
    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


      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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://177457]
[Your Mother]: Corion: I have lost the link to the pmdev guide(s). Do you have that handy?
[karlgoethebier]: Discipulus: see per lop please how to avoid the classic <c>perl 'rm -rfv *|'</>
[Eily]: karlgoethebier: more like grata non persona :P
Discipulus personae non gratae..
[Corion]: Your Mother: Hmm - I have the pmdev nodelet, which has some links, but I'm not sure what links/guides there are
[Corion]: pmdev wiki maybe?
[Discipulus]: uh neat karl!

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (12)
As of 2017-07-21 08:13 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (319 votes). Check out past polls.