Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: Using expressions in arrays for pattern matching

by yosefm (Friar)
on Dec 01, 2003 at 22:20 UTC ( #311421=note: print w/replies, xml ) Need Help??

in reply to Using expressions in arrays for pattern matching

I think the major issues are discussed. So I'll just add one important thing that could save you a lot of time in the future. Since you say you are a new programmer - you're lucky to get to know about this early and pick up good habits.

What I'm talking about is coding readably. It might sound unimportant, but believe me, when your programs will start getting longer, even you won't be able to quickly remmember what you wanted to do in lines 20-35 after working for a while on line 315-377 - not to mention different files. If you'll work in a team you'll even cause more casualties.

The first and foremost is to give meaningful names to variables. $i is customarily a loop counter, but if your loop gets longer, nobody will be able to tell that $i holds the file name without some serious reading, sometimes not even you. You don't have to use Java-like names like $fileNameVariableIUseTemporarily :) but it's a lot better to write:

foreach my $param (@params) { ... } foreach my $file (@files) { ... }

instead of

foreach $x (@params) { ... } foreach $i (@files) { ... }

Secondly - indentation. Ok, I can follow your small script here, but what if I get a code with 3-4 nesting levels over 40-50 lines? You'll soon start banging your head against the wall if your indentation will fail you...

Even worse, you sometimes get a block of closing braces - like the last four lines of your script. In really big blocks I comment them so I'll know what ends what (just like good old BASIC where putting the counter name at the end of a FOR loop was standard syntax).

Phew... pretty long. But you'll thank me later :) I had more than my fair share of unreadable code and I'm trying to save you the bother.

And now for something completely different: why

$x=$&; print OUTPUT "$x\n";
when you can print OUTPUT "$&\n"; ?

That's it. Have fun learning programming!

Replies are listed 'Best First'.
Re: Re: Using expressions in arrays for pattern matching
by thor (Priest) on Dec 01, 2003 at 23:23 UTC
    All good points on style. I'll mention perltidy here, as it will do many of these things for you automagically. I like editing a largish script, paying no heed to indentation and such, running it through perltidy and having pretty code come out.


      I think, though, that it's just as important to be good with readability while coding as after you're done. I know I don't program perfectly, and I know I've caught quite a few errors by indenting as-I-type and such things.

      Who is Kayser Söze?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://311421]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2017-04-25 17:47 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (461 votes). Check out past polls.