πŸ“˜ Anonymous subs in Perl 6

Let’s look at the creation of anonymous subs. One of the options (there are more than one) is to use syntax similar to what you often see in JavaScript.

say sub ($x, $y) {$x ~ ' ' ~ $y}("Perl", 6);

The first pair of parentheses contains the list of formal arguments of the anonymous sub; the second, the list of the arguments passed. The body of the sub is located between the braces. (The tilde denotes a string concatenation operator in Perl 6.)

By the way, it is important that there be no spaces before parentheses with the actual values of the sub parameters.

Another way of creating an anonymous sub is to use the arrow operator (->). We will discuss it later in the section dedicated to anonymous blocks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s