Functions of Named unary and list Operators in Perl
Syntactically, function calls are actually operators. They evaluate from right to left, and the return value can be a term.
There are basically two classes of function-call operators:
List operators
Named unary operators
List Operators
Functions like print, which take a list as an argument, are list operators. Because they take lists as arguments, they operate on all list items to the right of the function call unless otherwise restrained. So if a print statement contained a function that operated on a list and no parentheses were applied, the "inner" function would apply itself to every element to its right. For example, in the following diagram below, the printfunction indicates what would be printed and join indicates what would be joined.
In the examples below, notice how the absence of parentheses changes what gets printed.
In the first statement, the join puts a colon between each element in the array first, then the print statement results in:
"string ","a:b:c:d:e:f",
"other things","\n"
being printed.
The example above would normally appear on a single line. It is broken into multiple lines for readability.
In the second statement, the join puts a colon between each element in the array and the following two strings, then the print statement results in:
"string ",
"a:b:c:d:e:f:other things:\n"
being printed. The example above would normally appear on a single line. It is broken into multiple lines for readability.
Functions like uc (uppercase), which expect only one argument, are syntactically named unary operators.
That means that they take one argument, and one argument only. So in this example, no parenthesis are necessary:
print "string ", uc "another ",
"string\n";
The only string that will be uppercased is "another " because uc is not a list operator.
Functions that are list operators
It is beyond the scope of this course to describe every one of Perl's built-in functions (there are over 200 of them), but here's a list of all the functions that are list operators.
This should be useful for you to know when you will and will not need to use parenthesis.
Functions that are list Operators
All the functions listed here are list operators; any functions you do not see here are not list operators.
chmod LIST
printf LIST
chomp LIST
chop LIST
chown LIST
die LIST
exec LIST
formline PICTURE, LIST
grep BLOCK LIST
grep EXPR,LIST
join EXPR,LIST
kill LIST
map BLOCK LIST
map EXPR,LIST
pack TEMPLATE,LIST
print FILEHANDLE LIST
print LIST
printf FILEHANDLE LIST
push ARRAY,LIST
return LIST
reverse LIST
sort SUBNAME LIST
sort BLOCK LIST
sort LIST
splice ARRAY,OFFSET,LENGTH,LIST
sprintf FORMAT,LIST
syscall LIST
system LIST
tie VARIABLE,CLASSNAME,LIST
unlink LIST
unshift ARRAY,LIST
utime LIST
warn LIST
In Perl 6 (now known as Raku), the concept of function-call operators is more structured than in Perl 5, but the categorization into "list operators" and "named unary operators" isn't explicitly used in the Raku documentation. Instead, Raku focuses on function calls, subroutines, and methods with a clear and consistent syntax. However, the two categories you mentioned can be interpreted as follows:
List Operators
These are functions or operators that can take a list of arguments.
Examples include say, print, map, grep, join, and similar functions.
Syntax:
say "Hello, World!"; # say takes a list of arguments
my @result = map { $_ * 2 }, 1, 2, 3; # map operates on a list
These operators/functions typically process multiple arguments or iterate over lists.
Named Unary Operators
These are functions or operators that act on a single operand.
Examples include not, +, -, and ?.
Syntax:
say +42; # Unary plus
say -42; # Unary minus
say not True; # Negation
Named unary operators can also include custom subroutines that are defined to accept a single argument:
sub double($x) {
$x * 2;
}
say double(5); # Output: 10
Key Differences from Perl 5
In Raku, the distinction between operators and functions is more syntactically consistent, and you don't have the same ambiguity between "list operators" and other types of function calls as in Perl 5.
Parentheses are optional when calling functions with clear precedence rules, which aligns well with the perception of "list operators."
While your categorization can loosely apply to Raku, it's more common to discuss Raku's functions and operators in terms of their arity (number of arguments), precedence, and associativity rather than dividing them into "list" and "named unary" categories.