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

Reactive Programming

In the previous chapter, we talked about functional programming. Perl 6 is a multi-paradigm language that has built-in support for that. In this chapter, we talk about reactive programming, also known as functional reactive programming or event-driven programming. Again, Perl 6's core supports programming using this style out of the box.

The following topics will be covered in this chapter:

  • Concepts of reactive programming
  • On-demand and live supplies
  • Filtering and transforming data streams

What is reactive programming?

In procedural programming, the program lists instructions of which and when the variable gets a particular value, or when a block of code is executed. For example, a variable gets its value as a sum of two other integer variables:

$z = $x + $y;

If either $x or $y has been changed after that assignment, nothing changes with the value of $z. Another example—a value returned by a function is assigned to a variable:

$area = area-of-circle($r);

Although it is clearly seen from the code that it calculates the area of a circle with the given radius, if the $r variable is changed, you have to manually update the value of $area.

Reactive programming aims to change that "static" behavior of dependent values. Interactive interfaces of many computer programs and web pages are good examples of a reactive approach. Imagine an online calculator...

On-demand supplies

The data flow of supplies contains two parts—the supplier that emits data and the tap that receives it. Perl 6's reactive programming model is a thread-safe implementation of the Observer design pattern.

Let us create our first on-demand supply using the supply keyword:

supply {
emit($_) for 'a'..'e';
}

The supply is here but it does not emit any data yet because there is no demand. You can easily see this if you add a print instruction to the loop:

supply {
for 'a'..'e' {
emit($_);
say "Emitted $_";
}
}
sleep 2;

The program just silently quits after 2 seconds.

To make the supply generate data, we need to create a tap. The supply block returns a value of the Supply type, and you can call the tap method on it to pass the code that will be executed in response to the data emitted...

Live supplies

Live supplies generate data regardless of the number of taps. Unlike on-demand supplies, if there are no taps open, emitted data is still generated and it simply disappears. As soon as the tap is open, it starts receiving data from that moment; all the history is lost.

To create a live supplier, call a constructor of the Supplier class. A tap must be connected to the supply, returned by the Supply factory method. This is all shown in the following example:

my $supplier = Supplier.new;

$supplier.Supply.tap({
say $_;
});

$supplier.emit($_)
for 'a'..'e';

You may be a bit confused by the presence of both the Supply and Supplier classes. The Supplier class is a factory to generate live supplies.

Let us see how live supply streams data and what happens when no taps are open. In the program below, a live supply generates data in a separate thread created...

Summary

In this chapter, we were talking about reactive programming in Perl 6. This paradigm has support in the language core, so no external modules are needed to start programming. The supply is the main character in this chapter—we covered two different types of them, on demand and live supplies. We had many examples of attaching taps to supplies and saw how the data flow is organized and how it can be filtered.

* * *

This was the last chapter of the book. In 15 chapters, we went from the very basics of Perl 6 through an object-oriented approach and concurrent programming to higher order features such as functional and reactive programming. Perl 6 naturally embeds all of that. There is no doubt that more than 15 years of development added value to the quality and maturity of the language in general.

During the process of writing this book, which took about half a year...

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