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

Modules

In the previous chapter, we talked about subroutines. Subroutines make code more readable and easier to reuse.

The following topics will be covered in this chapter:

  • Organizing code in modules
  • Loading modules
  • Exporting names
  • Introspection
  • Module location
  • Installing and uninstalling modules

Creating a module

Modules in Perl 6 serve the purpose of keeping code in separate files. It may be a simple library consisting of a couple of functions developed by you, or it may be a big collection of classes that is developed by an external company. In any case, if you use a module, you get the power of the previous work and have an interface to reach that functionality.

In this chapter, we will talk about organizing code in modules and using them in a program.

Let us create our first module and let's take the simple task of adding numbers that we were exploiting in earlier chapters, for example, in Chapter 2, Writing Code.

So, we have an add function for adding up two numbers and the code that uses it:

sub add($a, $b) {
return $a + $b;
}

my $a = 10;
my $b = 20;
my $sum = add($a, $b);
say $sum; # 30

Our current goal is to put the code of the add function into a separate...

Using modules

In the previous section, we created a simple module with a single function in it, and used the module in a program. In this section, we will examine other methods of how Perl 6 can load the module and its functions.

There are four keywords that we are going to explore— use, need, require and import. They all are used in the context of loading modules but behave a bit differently.

Using a module assumes at least two things—first, the module file has to be found and compiled; second, the names from the module (such as subroutines or variables) should become visible to the program.

The need keyword

The need keyword loads a module at compile time but does not import the names from it...

Introspection

Perl 6 modules contain a mechanism that allows you to get information about the content of the module. Obtaining this meta-information is called introspection.

Take the Math.pm modules from the previous section, More on is export, as an example. This is how we can list all the methods that are exported by that module:

use Math;

say Math::EXPORT::.keys;

This refers to the default EXPORT sub, which the compiler generated for us. The sub returns an object of the EXPORT type that implements the Perl6::Metamodel::PackageHOW interface. We'll not go deep into that theory and will limit ourselves to calling a useful method keys that gives us a list of the tags available in the module:

(plusminus muldiv ALL)

Having the list of tags, we can iterate over them to get the list of subroutines that belong to them:

use Math;

say Math::EXPORT::plusminus::.keys;
say Math::EXPORT...

Using zef

Please note that this section describes a tool that is specific to the Rakudo Star distribution. At the time of writing, Rakudo is the only production-ready Perl 6 compiler available on the market. If you happen to use other compilers, please check their documentation on how to install modules.

The Rakudo Star distribution comes with a handy module management tool called zef. Notice that it has been part of the distribution since Rakudo Star version 2017.01. Earlier releases include another tool, panda, which has now become obsolete.

zef is a command-line tool written in Perl 6. After installing Rakudo Star, its executable file will be in the Rakudo/share/perl6/site/bin directory. The Rakudo installer also fixes the PATH environment variable so that it contains a correct path to the directories with executable files of the distribution.

This tool uses the ecosystem of...

Summary

In this chapter, we went through the main steps of working with modules. First, we saw how to create a module and how to tell the compiler where to find it. Then, we examined different ways of loading modules and importing names from them. Finally, we went to a Rakudo-specific module management tool, zef, and used it to install and uninstall modules and looked at the internal storage that Rakudo uses for saving the modules on disk.

In the next chapter, Chapter 8 , Object-Oriented Programming, we will talk about classes, which share some common elements with modules in how the code is located in separate files.

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