3. Style: space, (), {}

Perl is a permissive language. Space can be used and also left out. In this chapter I'll discuss the use of space and brackets.

3.1 Parentheses

3.1.1 No space before parentheses around function arguments.
() or round brackets or parentheses can be used and also left out. However, not always! I suggest:

1. use always parentheses around the arguments in function calls (built-in or user-defined)
2. use no space between function name and arguments
print("This!"); # instead of print "This!";
print("This!"); # instead of print ("This!");
Following these rules will avoid problems when using a function as an argument of a function:
print(add(2, 3)); # is equal to print(add 2, 3);
print(add(2, 3), 5); # is different from print(add 2, 3, 5); suggesting three arguments instead of two!
Sometimes I prefer a space after the left parenthesis and before the right one: space around a 'complex' script (inside brackets). It's of course a matter of taste. Compare:
print(reverse(1, (2, 3), (4 .. 7)));
print( reverse(1, (2, 3), (4 .. 7)) ); # I do like this one
print( reverse( 1, (2, 3), (4 .. 7) ) );
Remember: parentheses always work!
3.1.2 Always a space before parentheses in keywords and operators.
Use a space between the next keywords/operators -among others- and the opening parenthesis:
my local our and or eq ne if else elsif until unless while for foreach return q qq qw qx
So, the following examples are correct:
if (condition) # instead of if(condition)
foreach (@array) # instead of foreach(@array)
3.1.3 Always a space around operators in general:
Use a space before and after operators like arithmetic, equality, assignment operators:
+ - * / % ** == != <=> > < >= <= = += -= *= /= %= **=
So, the following examples are correct:
$name = "Reinier";
if (4 > 3)
$x += 2;

3.2. Fast always a space before {...} or curly brackets (braces)

This rule is simple:

1. space before left curly brace of oneline or multiline block {...}
2. specifically, space between identifier and left curly brace in a subroutine definition
if (condition) {...} # instead of if(condition){...}
sub identifier {...} # instead of sub identifier{...}
Exceptions in case of variables.

1. if you want to add a character to the value of a scalar variable (string):
$bird = "penguin";
$observation = "I saw two ${bird}s.";
2. if you want to dereference a reference to e.g. a scalar variable:
$scalar = "This is a scalar";
$scalar_ref = \$scalar;
print "Reference: " . $scalar_ref . "\n";
print "Dereferenced: " . ${$scalar_ref} . ";\n"

3.3. More on space

Finally:

1. use a space after each comma.
2. use no space before the semicolon.

Special: the datatype 'list' requires always parentheses

When defining the datatype 'list', you should be aware of the necessary parentheses! It's sometimes confusing (forget the use of the operator 'my' for now, but notice the space before the opening parenthesis):
my $var; # declares a scalar variable (which contains single value)
my ($var); # declares a list with one scalar
Notice the following:
my ($var1, $var2); # declares two variables
(my $var1, $var2); # declares only the first variable and gives compilation errors when using the recommended pragma 'use strict'
and (the following codes make more sense after studying the chapters of Part 1). Notice that I use no space after the operator qw
@abc = qw(a b c);
($var) = @abc; # defines a list
print($var); # prints a, i.e. the first element of the array @abc
@abc = qw(a b c);
$var = @abc; # defines a scalar
print($var); # prints 3, the number of elements of the array @abc
Be careful!

Yes, I've to admit that I -when in a hurry- omit parentheses if allowed. So I'll carefully scan all texts on these issues and correct them!