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

Input and Output

This chapter is devoted to input and output, which is mostly based on the IO::Handle class in Perl 6. Computer programs in general communicate with the user. It may either be the input and output in a console application, or reading configuration files, or saving results in a file on disk. In this chapter, we will talk about the input and output facilities in Perl 6.

The following topics will be covered in this chapter:

  • Standard input and output
  • Working with files
  • Analyzing the properties of files and directories
  • Methods for reading from input streams
  • Methods for writing to output streams
  • Formatted output

Standard input and output

In the previous chapters, we have created many programs that print to the console and read data from it. Let us refresh some knowledge from Chapter 2, Writing Code, and create a program that asks for the user's name and greets them:

my $name = prompt 'What is your name? ';
say "Hello, $name!";
note "Greeted $name at " ~ time;

Here, the prompt function prints the message and waits until the user enters a string. The string is saved in the $name variable, which is later interpolated in a string in double quotes. The note function prints the debugging message and logs the time of when the person was greeted.

In this program, Perl 6 uses two standard communication channels, the standard input stream (stdin for short) and the standard output stream (stdout). These are the default streams that receive the user's input...

Working with files and directories

Working with files in Perl 6, as well as in many other languages, is done via file handles. You get the file handle as soon as you open a file; later, you use the handle to write to a file or to read from it. All the other operations, such as flushing a buffer or closing a file, are also performed via the handle.

Opening a file

To open a file, use the open function (it is supplied by the IO role but can be used as a simple built-in function). It takes the path to the file and a number of optional parameters. The return value is a file handle, as shown:

my $fh = open '/etc/passwd';

By default, the file is opened in the read-only mode. It is possible to pass the mode name explicitly...

Reading from a stream

There are many different methods that the IO::Handle class offers us for reading from streams. We already have seen a few in the section Simple input and output in Chapter 2, Writing Code. Here, we'll discuss them in detail and see other alternatives.

Reading a single line

We start with the get method, which reads a line from the input stream. For example, to read a line from the standard input, call the method on the $*IN instance, as shown in the next example:

my $line = $*IN.get;
say $line;

The program waits for you to enter some text. After the line is complete and the 'Enter' key is pressed, the get method returns control to the program and then the line is printed to the screen. Alternatively...

Writing to a stream

In this section, we will examine methods that the IO::Handle class offers for writing to a stream.

The print  function

We will start with the simple print function. Basically, its usage is obvious. It prints the text to the stream. In the case of standard output, use the bare print function or the $*IN.print method. If you work with a file, use its file handle.

The following program creates a file named hello.txt and writes a string to it.:

my $fh = open 'hello.txt', :w; # Open a file for writing
$fh.print('Hello, World');     # Print to the file
$fh.close;                     # Close the file so that the data is saved

If the file already exists, it will be re-written, and...

Summary

In this chapter, we talked about input and output facilities that are available in Perl 6. The IO::Handle class provides the universal way of working with standard input and output streams as well as with files using the same interface. We discussed how to create files and how to test different properties of files and directories and examined various methods of reading and writing.

When working with files, you may sometimes be faced with exceptional situations; we've seen a few examples in this chapter. In the next chapter, we will discuss exceptions in Perl 6 in detail.

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