mhinkle has asked for the
wisdom of the Perl Monks concerning the following question:

Hello,
I have just started to learn Perl. I am not a programmer (yet).

All of the examples I find have no spaces between lines and I am getting older so its harder for me to see and thus understand, :( so I’d like to add spaces (newlines, whitespace) in my programs—is this acceptable?

I am including an example below of a program example I found and the way I have modified it to make it easier for me to understand and see.

Can someone tell me if these two scripts will produce the EXACT same results?
Or if my modifications change the functionality of it?

Perl is a free-form language: you can format and indent it however you like. Whitespace serves mostly to separate tokens, unlike languages like Python where it is an important part of the syntax, or Fortran where it is immaterial.

Great. This is a good place to start, and you've picked a fun language to learn with.

Since you're just getting started, go to http://perldoc.perl.org and read perlintro. The question you're asking is addressed in the section titled, "Basic Syntax Overview". There it explains the rules regarding whitespace. But do read through that full document. It will hasten your learning experience, and takes advantage of the fact that many of the questions you're likely to come up with in the first few days have already been answered in one concise document.

While I cannot see the changes you made in your example (a problem with node formatting), the answer is yes, you can use whitespace. In fact, please add (a reasonable amount of) whitespace. Separate any logical blocks of code you have with an extra linebreak. I generally sprinkle one after (a group of) variable declarations, too. (But I hate "double-spaced" code.)

Also, use tabs to indent your code. Configure your editor to show them as n spaces, where n is a comfortable value (for you). I generally use a value of two spaces, but whenever I'm reading code I'm unfamiliar with, I'll increase it temporarily to four. It helps me understand the flow better.

You might also like the /x modifier if you're working with regular expressions. And if you don't have a syntax-colouring editor yet, get one immediately! It will help tremendously.

Here's what my code looks like, to give you a rough idea on what I consider OK spacing:

Some editors have “Perl Tidy” tools that will automatically reformat source-code without changing it, in order to improve visual consistency within the code. Yes, such changes can be a big improvement, because we are extremely visual creatures.

One thing can be a problem, though ... if you reformat an entire module, a source code control system will perceive that the entire module has been changed. Therefore, what I often prefer to do is to highlight a particular block of code, reformat it automatically, then check-in the file with an appropriate comment. Then, separately, make the changes that I am contemplating, and check those in again. This creates a much clearer history of what was changed and why. Gradually, over the course of time, but only in the areas currently receiving attention for other reasons, the visual quality of the code will improve.