Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

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

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

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

I personally hate this style script:

#!perl my $var = ...; my @var = ...; sub1(); sub2(); exit 0;

I think this leads to a lot of global variable usage and sloppy code in general. I personally prefer using a single entry point, and keeping everything in there

#!perl main(); sub main { my $var = ...; my @var = ...; sub1($var); sub2(@var); exit 0; }

Don't know about you, but I find this less error prone.

I also think that the $$hash{ element } notation is confusing.... It's not that much different, but I think @{ $hash->{ element } } would be easier to read than  @{ $$hash{ element } } or some such thing.

Replies are listed 'Best First'.
Re: Re: Some suggestions on coding style - a chance to critique
by emilford (Friar) on Jun 26, 2002 at 15:40 UTC
    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.

      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 ); }
        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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://177405]
usemodperl enters an empty gate

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2018-06-23 18:58 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.