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

Working with Variables and Built-in Data Types

Perl 6 is a language with gradual typing. This means that you are not required to indicate the type of the variables you create: you may freely use the same variable to store data of different types. However, you may also create a typed variable and, in that case, the compiler will check the usage of that variable and make sure that the variable is only used in the operations allowed for that type.

In this chapter, we will first go through the built-in types of Perl 6 and, later, learn how to work with variables:

  • Built-in data types
  • Type hierarchy
  • Variables
  • Scalars, arrays and hashes
  • Object-oriented properties of data types
  • Simple and composite data types

Using variables

In any programming language, variables are named pieces of memory that you can use to store a value as well as retrieve it. In Perl 6, a variable is a container that can host a value of one of the types, either built-in in the language or created by the user.

Declaring variables

Every variable must be declared before its use in the program. You don't need to declare all the variables at the beginning of the program. From a practical perspective, the point of declaration can (and should) be as close as possible to the place where it is first used. The most practical reason to do that is to make the visibility of the variable better—if you declare too early, you force the reader of your program to...

Using simple built-in data types

Perl 6 comes with a number of various built-in types that cover the common range of things, such as Booleans, integers, and strings, but also offers unusual data types. We will cover them in this section. To demonstrate the built-in types, we will print them to the console using the say function, as we did in the ‘Hello, World!’ example.

The hierarchy is built using two types of items: roles and classes. Roles are drawn in ovals, while classes are rectangle boxes. Roles are similar to interfaces in some programming languages. In this chapter, we won't focus on the details of what is a role or a class. You can learn that in detail in Chapter 8, Object-Oriented Programming. For now, we will assume that you have some basic understanding of object-oriented programming and will be able to understand the hierarchy of the data types...

Data types to manipulate date and time

Perl 6 offers built-in support to work with dates and times. This is very handy because date-time calculations are not easy (you need to think about leap years, extra seconds, time zones, calendar corrections, and so on).

We will cover two classes: Date and DateTime.

Using the Date class

The Date class represents the date—a collection of three numbers, year, month, and a day of the month. To create a new date, call a constructor with the three values:

my $date = Date.new(2017, 7, 19);
say $date; # 2017-07-19

To create a variable based on today's date, use the today method, as follows:

my $today = Date.today;
say $today; # 2017-07-17

To clone a date, call clone,...

Summary

In this chapter, we looked at an overview of the built-in data types in Perl 6 and learned how to work with variables. The most important fact is that variables in Perl 6 are instances of the different built-in data type classes. The details of those classes are located in Chapter 8, Object-Oriented Programming, but some elements of the object-oriented programming are needed to successfully create and use variables in Perl 6.

In the first part of the chapter, you learned three structural types of variable containers—scalars, arrays, and hashes—, and examined their main methods. In the second part, we took an in-depth look at different data types, such as integer, rational, floating-point numbers, strings, dates, and times.

In the next chapter, we will continue with observing the flow control in Perl 6 programs.

...
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