Following these rules will avoid problems when using a function as an argument of a function:print("This!"); # instead of print "This!";
print("This!");# instead of print ("This!");
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(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!
Remember: parentheses always work!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) ) );
So, the following examples are correct:my local our and or eq ne if else elsif until unless while for foreach return q qq qw qx
if (condition) # instead of if(condition)
foreach (@array)# instead of foreach(@array)
So, the following examples are correct:+ - * / % ** == != <=> > < >= <= = += -= *= /= %= **=
$name = "Reinier";
if (4 > 3)
$x += 2;
Exceptions in case of variables.if (condition) {...} # instead of if(condition){...}
sub identifier {...}# instead of sub identifier{...}
2. if you want to dereference a reference to e.g. a scalar variable:$bird = "penguin";
$observation = "I saw two ${bird}s.";
$scalar = "This is a scalar";
$scalar_ref = \$scalar;
print "Reference: " . $scalar_ref . "\n";
print "Dereferenced: " . ${$scalar_ref} . ";\n"
Notice the following:my $var; # declares a scalar variable (which contains single value)
my ($var);# declares a list with one scalar
and (the following codes make more sense after studying the chapters of Part 1). Notice that I use no space after the operator qwmy ($var1, $var2); # declares two variables
(my $var1, $var2);# declares only the first variable and gives compilation errors when using the recommended pragma 'use strict'
@abc = qw(a b c);
($var) = @abc;# defines a list
print($var);# prints a, i.e. the first element of the array @abc
Be careful!@abc = qw(a b c);
$var = @abc;# defines a scalar
print($var);# prints 3, the number of elements of the array @abc