Reader small image

You're reading from  Perl 6 Deep Dive

Product typeBook
Published inSep 2017
Reading LevelIntermediate
PublisherPackt
ISBN-139781787282049
Edition1st Edition
Languages
Right arrow
Author (1)
Andrew Shitov
Andrew Shitov
author image
Andrew Shitov

Andrew Shitov has been a Perl enthusiast since the end of the 1990s, and is the organizer of over 30 Perl conferences in eight countries. He worked as a developer and CTO in leading web-development companies, such as Art. Lebedev Studio, Booking dotCom, and eBay, and he learned from the "Fathers of the Russian Internet", Artemy Lebedev and Anton Nossik. Andrew has been following the Perl 6 development since its beginning in 2000. He ran a blog dedicated to the language, published a series of articles in the Pragmatic Perl magazine, and gives talks about Perl 6 at various Perl events. In 2017, he published the Perl 6 at a Glance book by DeepText, which was the first book on Perl 6 published after the first stable release of the language specification.
Read more about Andrew Shitov

Right arrow

Writing Code

This chapter will prepare you to write Perl 6 code. Before we examine variables, data types, object orientation, and other important topics, let's learn more about how the source code is organized in Perl 6. There are many elements that differ from other programming languages, and it is worth spending time getting used to it.

In this chapter, we will cover the following topics:

  • Using Unicode
  • Whitespaces and unspaces
  • Comments
  • Creating Pod documentation
  • Phasers
  • Simple input and output

Using Unicode

The default assumption is that the source of a Perl 6 program uses UTF-8. It gives you the power of the whole spectrum of characters without worrying if it will work. In Perl 5, for example, you had to add special instructions in order to inform the interpreter that you are using non-ASCII characters in the source code. In Perl 6, this is much easier.

First, Unicode characters may be freely used in strings. For example, let's try some Greek and Chinese graphemes, as shown in the following lines of code:

say 'C = 2πr';       # Circumference of a circle
say         # 'Sun' and 'Moon' give 'bright'

The preceding two lines of code will print the corresponding strings as expected:

C = 2πr 

Alternatively, it is possible to refer to the Unicode codepoints by their names. For example, consider the following line of...

Whitespaces and unspaces

As we just saw, a Perl 6 program can intensively use the Unicode characters outside of the conventional ASCII set. This also applies to the whitespaces. Whitespaces are those gaps between the elements of the program, that are traditionally represented by spaces (ASCII code 0x20), tabs (0x09), and newlines (a single line feed character 0x0A in Unix and a series of two characters, carriage return 0x0D, and line feed 0x0A in Windows). Perl 6 extends the concept of whitespaces and accepts Unicode whitespace in every place of the code where a regular space is allowed. Be careful when you work with an existing code that, for some reason, is filled with Unicode characters.

A whitespace character set in Perl 6 includes characters that have one of the following Unicode properties:

  • Zs: Separator, Space
  • Zl: Separator, Line
  • Zp: Separator, Paragraph

You can find...

Comments

In Chapter 1, What is Perl 6?, we've seen two ways of writing programs—by passing it in the -e command-line option or by saving it in a file. Of course, all big programs are stored in files. In this section, we will see how Perl 6 helps the programmer to organize the code better by adding comments.

Comments are parts of the source code, which are ignored by the compiler. Comments are intended to give additional information about the program itself. Good examples of comments are explanations of the algorithms used, or the purpose of variables, or a description of the input arguments of a function.

Perl 6 offers two ways of saving additional human-oriented information in the source code by using comments and the so-called Pod. First, we will examine the comments.

Comments are fragments of source code that a compiler does not consider as direct instructions for...

Creating Pod documentation

Pod is a sublanguage inside Perl 6 used to write documentation. It can be considered as an extended version of comments, which allows more functionality in expressing the content. Pod in Perl 6 is the evolution of the POD (Plain Old Documentation) in Perl 5. In Perl 6, the name is not an abbreviation and thus is not capitalized. In this section, we will examine the syntax of the Pod markup language.

Pod content is placed in the same source file as the Perl 6 program itself. A Pod section is a series of lines containing some textual information. The compiler is switching between parsing Pod and Perl 6 when it sees the beginning and ending marks of the Pod section. Pod syntax is designed to express the semantics of the documentation and to help organize it in a more structured way.

Let's see how you create a Pod block. There are a few types of Pod...

Phasers

When creating a program in Perl 6, it is important to understand that controlling the program flow is a little trickier than simply following the instructions of the code. There are special types of code blocks that are automatically called by the compiler at different phases of the compilation and the execution processes. Those blocks are called phasers.

We mentioned two of them, BEGIN and CHECK, in Chapter 1, What is Perl 6?, when we talked about the -c command-line option of the compiler. Now, let's take a look at the rest.

Syntactically, phasers are blocks of code in curly braces preceded by a phaser name. The following table summarizes the different phasers that exist in Perl 6. Some of the phasers are executed at compile-time before the rest of the program is compiled and executed. Some are called at runtime.

Phaser name Execution stage Actions
BEGIN compile...

Simple input and output

In the previous examples, we used the built-in print and say functions to print something to the console (speaking more strictly, to the standard output attached to the program). In this section, you will learn how to perform basic reading from the standard input. This is basically how the program gets what you type onto the console.

To read the input, there are a few functions that you may use directly without loading any modules. They are listed in the following table:

Function What it does
get This reads one line from the input and returns it
lines This returns the list of lines containing the data lines that came from the standard input
slurp This returns a string that contains the whole input

The get and line functions may be used when you need to parse the input data line by line. For example, call get as many times as you need if you...

Summary

In this chapter, we looked at the way a Perl 6 program is organized. The source code is written in Unicode, and there are many Unicode elements in the syntax of the language that may be used to make the program more expressive. We also examined how to create and use comments that vary from one-line notes to bigger Pod blocks that may contain documentation about the program. We looked at that ways the parts of the source code may be placed in different phasers to change the flow of the program. And finally, you learned a method for getting input from the users.

Now, we are ready to create real Perl 6 programs. In the next chapter, we will talk about the data types in Perl 6 and how to use variables.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Perl 6 Deep Dive
Published in: Sep 2017Publisher: PacktISBN-13: 9781787282049
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Andrew Shitov

Andrew Shitov has been a Perl enthusiast since the end of the 1990s, and is the organizer of over 30 Perl conferences in eight countries. He worked as a developer and CTO in leading web-development companies, such as Art. Lebedev Studio, Booking dotCom, and eBay, and he learned from the "Fathers of the Russian Internet", Artemy Lebedev and Anton Nossik. Andrew has been following the Perl 6 development since its beginning in 2000. He ran a blog dedicated to the language, published a series of articles in the Pragmatic Perl magazine, and gives talks about Perl 6 at various Perl events. In 2017, he published the Perl 6 at a Glance book by DeepText, which was the first book on Perl 6 published after the first stable release of the language specification.
Read more about Andrew Shitov