DavidSzczesniak
1/9/2018 - 10:09 AM

Function Parentheses

Parentheses in Perl are always optional, as long as they don't change the meaning of that line of code. A good rule to go by is - "If it looks like a function call, it is a function call". However, something important to keep in mind about functions is that they will only operate on the arguments given inside parentheses.

# Example with print, it has optional parentheses
# Two ways to print the same thing:
print("Hello World!\n");
print "Hello World!\n";

# Function call using print:
print(2+3); # prints 5, but its return value is 1 or 0 - indicating if the print succeeded or not.

# Therefore:
print (2+3)*4 # will print 5, not 20
# it takes the return value, 1, and multiplies that by 4 instead, but throws it away as you didn't tell it to do anything with that value.

# The correct way:
print ((2+3)*4) # add 2 and 3, then multiply the product by 4