Skip to Content
Think Perl 6
book

Think Perl 6

by Laurent Rosenfeld, Allen B. Downey
May 2017
Beginner
466 pages
11h 12m
English
O'Reilly Media, Inc.
Content preview from Think Perl 6

Chapter 4. Loops, Conditionals, and Recursion

The main topic of this chapter is the if statement, which executes different code depending on the state of the program. But first I want to introduce two new operators: integer division and modulo.

Integer Division and Modulo

The integer division operator, div, divides two numbers and rounds down to an integer. For example, suppose the runtime of a movie is 105 minutes. You might want to know how long that is in hours. In Perl, conventional division returns a rational number (in many languages, it returns a floating-point number, which is another kind of internal representation for noninteger numbers):

> my $minutes = 105;
> $minutes / 60;
1.75

But we don’t normally write hours with decimal points. Integer division returns the integer number of hours, dropping the fraction part:

> my $minutes = 105;
> my $hours = $minutes div 60;
1

In arithmetic, integer division is sometimes called Euclidean division, which computes a quotient and a remainder.

To get the remainder, you could subtract off one hour in minutes:

> my $remainder = $minutes - $hours * 60;
45

An alternative is to use the modulo operator, %, which divides two numbers and returns the remainder:

> my $remainder = minutes % 60;
45

The modulo operator is very common in programming languages and is more useful than it seems. For example, you can check whether one number is divisible by another—if $dividend % $divisor is zero, then $dividend is divisible by $divisor. This is commonly ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Learning Perl 6

Learning Perl 6

brian d foy
Perl 6 Deep Dive

Perl 6 Deep Dive

Andrew Shitov
Perl Best Practices

Perl Best Practices

Damian Conway

Publisher Resources

ISBN: 9781491980545Errata Page