Printing to the console/terminal is normally without problems. However, it could happen that the warning 'print (...) interpreted as function' appears. It is an unnecessary warning, which you can avoid when removing the space between 'print' and the first parenthesis. Another option is to use an extra pair of parentheses before 'print' and after the last parenthesis. So, if it happens, consider.
Or leave all parentheses (I don't like...)print(expression); instead of print (expression);
(print (expression)) instead of print (expression);
print expression; instead of print(expression);
print("abc"); # a list with one string, i.e. a group of characters between apostrophes (1)
Although Perl enables to leave out the parentheses, I'll use them for good reasons (2).print("abc", "---", "def"); # a list with three strings; one-string-alternative: 'print("abc---def")'
A list with a math operation:print(1, 2, 3); # a list with three numbers (integer (3)); output: 123
print(1, " ", 2, " ", 3);# a list with three numbers and two spaces; output: 1 2 3
print("1 2 3");# a list with three 'numbers' and two spaces; output: 1 2 3
Note the difference:print(2 * 3); # output: 6
What did you mean?print("(2 * 3)"); # output: (2 * 3)
... or ...print(2 + 3 * 4); # output: 14
Use parentheses in a smart way! Avoid bad code like:print((2 + 3) * 4); # output: 20
Another math expression:print(2 + 3) * 4; # output: 5
A test on equality (modulo operation):print(4 > 3); # output: 1 which means TRUE. 0 means FALSE
print(0 == (4 % 2)); # the remainder of 4 divided by 2 equals 0; the equation is TRUE, so the output is 1
Evaluation from right to left: (2 * 3) results in the number 6, which is converted to a string. The two strings "2 * 3 is: " and "6" are concatenated. Or better, use commas:print("2 * 3 is: " . (2 * 3));
print("2 * 3 is: ", 2 * 3);
or without parentheses:$_="abcdef";
print();# the contents of $_, i.e. abcdef, will be printed
print; # no argument but the output is the value of $_
A special case of printing multilines of output is the so called HERE document:print("Line 1\nLine2\nLine3");
Alternative implementation without 'space-last-tag'-problem:print <<HAIKU;
The west wind whispered,
And touched the eyelids of spring:
Her eyes, Primroses.
R. M. Hansard, 1899
HAIKU# no whitespace before and after this last tag HAIKU !
To print blank lines: use '\n' and the repetition operator 'x' for strings$haiku_text = q{ The west wind whispered, And touched the eyelids of spring: Her eyes, Primroses. R. M. Hansard, 1899 }; print("$haiku_text\n");
print("Hello", "\n" x 3, "Hello again!"); # "\n" x 3 means three blank lines
What does this mean? Actually, two things:print(STDOUT ("abc"))
The string "abc" will be written to "my_datafile", only if the file "my_datafile" was opened successfully for writing (">" redirects MY_OUTPUT to "my_datafile").open (MY_OUTPUT, ">", "my_datafile") or die "Could not open file 'my_datafile' $!"; # (4)
print(MY_OUTPUT ("abc"));
close (MY_OUTPUT);
print("2" + "3"); # numeric addition with strings; output: 5
Study chapter 5. Contextprint((45/2) x 4); # string repetition with numbers; output: "22.522.522.522.5"
The functionuse 5.38.1; # necessary for 'say'; I use perl v5.38.1 say("hello!");# equivalent to print("hello!\n");