Pure Programmer

Functions

Functions1.pl
```#!/usr/bin/env perl
use utf8;
use strict;
use warnings;

sub printHello {
print "Hello, world!\n";
}

MAIN:
{
binmode(STDOUT, ":utf8");
binmode(STDERR, ":utf8");
binmode(STDIN, ":utf8");
printHello();
}

```
Output
\$ perl Functions1.pl Hello, world!
Functions2.pl
```#!/usr/bin/env perl
use utf8;
use strict;
use warnings;

sub printHello {
my (\$name) = @_;
print "Hello, ", \$name, "!\n";
}

MAIN:
{
binmode(STDOUT, ":utf8");
binmode(STDERR, ":utf8");
binmode(STDIN, ":utf8");
printHello("Fred");
printHello("Wilma");
printHello("Barney");
printHello("Betty");
}

```
Output
\$ perl Functions2.pl Hello, Fred! Hello, Wilma! Hello, Barney! Hello, Betty!
Functions3.pl
```#!/usr/bin/env perl
use utf8;
use Utils;
use strict;
use warnings;

sub squared {
my (\$j) = @_;
return \$j * \$j;
}

MAIN:
{
binmode(STDOUT, ":utf8");
binmode(STDERR, ":utf8");
binmode(STDIN, ":utf8");
for (my \$i = 1; \$i <= 10; ++\$i) {
print Utils::messageFormat("\{0:d\}^2 = \{1:d\}", \$i, squared(\$i)), "\n";
}
}

```
Output
\$ perl Functions3.pl 1^2 = 1 2^2 = 4 3^2 = 9 4^2 = 16 5^2 = 25 6^2 = 36 7^2 = 49 8^2 = 64 9^2 = 81 10^2 = 100
Functions4.pl
```#!/usr/bin/env perl
use utf8;
use Utils;
use strict;
use warnings;

sub exponentiation {
my (\$base, \$powerArg) = @_;
my \$power = \$powerArg;
my \$result = 1;
while (\$power > 0) {
\$result *= \$base;
--\$power;
}
return \$result;
}

MAIN:
{
binmode(STDOUT, ":utf8");
binmode(STDERR, ":utf8");
binmode(STDIN, ":utf8");
for (my \$b = 1.1; \$b < 2.05; \$b += 0.1) {
for (my \$p = 2; \$p <= 10; ++\$p) {
print Utils::messageFormat("\{0:f\} ^ \{1:d\} = \{2:f\}", \$b, \$p, exponentiation(\$b, \$p)), "\n";
}
}
}

```
Output
\$ perl Functions4.pl 1.100000 ^ 2 = 1.210000 1.100000 ^ 3 = 1.331000 1.100000 ^ 4 = 1.464100 1.100000 ^ 5 = 1.610510 1.100000 ^ 6 = 1.771561 1.100000 ^ 7 = 1.948717 1.100000 ^ 8 = 2.143589 1.100000 ^ 9 = 2.357948 1.100000 ^ 10 = 2.593742 1.200000 ^ 2 = 1.440000 1.200000 ^ 3 = 1.728000 1.200000 ^ 4 = 2.073600 1.200000 ^ 5 = 2.488320 1.200000 ^ 6 = 2.985984 1.200000 ^ 7 = 3.583181 1.200000 ^ 8 = 4.299817 1.200000 ^ 9 = 5.159780 1.200000 ^ 10 = 6.191736 1.300000 ^ 2 = 1.690000 1.300000 ^ 3 = 2.197000 1.300000 ^ 4 = 2.856100 1.300000 ^ 5 = 3.712930 1.300000 ^ 6 = 4.826809 1.300000 ^ 7 = 6.274852 1.300000 ^ 8 = 8.157307 1.300000 ^ 9 = 10.604499 1.300000 ^ 10 = 13.785849 1.400000 ^ 2 = 1.960000 1.400000 ^ 3 = 2.744000 1.400000 ^ 4 = 3.841600 1.400000 ^ 5 = 5.378240 1.400000 ^ 6 = 7.529536 1.400000 ^ 7 = 10.541350 1.400000 ^ 8 = 14.757891 1.400000 ^ 9 = 20.661047 1.400000 ^ 10 = 28.925465 1.500000 ^ 2 = 2.250000 1.500000 ^ 3 = 3.375000 1.500000 ^ 4 = 5.062500 1.500000 ^ 5 = 7.593750 1.500000 ^ 6 = 11.390625 1.500000 ^ 7 = 17.085938 1.500000 ^ 8 = 25.628906 1.500000 ^ 9 = 38.443359 1.500000 ^ 10 = 57.665039 1.600000 ^ 2 = 2.560000 1.600000 ^ 3 = 4.096000 1.600000 ^ 4 = 6.553600 1.600000 ^ 5 = 10.485760 1.600000 ^ 6 = 16.777216 1.600000 ^ 7 = 26.843546 1.600000 ^ 8 = 42.949673 1.600000 ^ 9 = 68.719477 1.600000 ^ 10 = 109.951163 1.700000 ^ 2 = 2.890000 1.700000 ^ 3 = 4.913000 1.700000 ^ 4 = 8.352100 1.700000 ^ 5 = 14.198570 1.700000 ^ 6 = 24.137569 1.700000 ^ 7 = 41.033867 1.700000 ^ 8 = 69.757574 1.700000 ^ 9 = 118.587876 1.700000 ^ 10 = 201.599390 1.800000 ^ 2 = 3.240000 1.800000 ^ 3 = 5.832000 1.800000 ^ 4 = 10.497600 1.800000 ^ 5 = 18.895680 1.800000 ^ 6 = 34.012224 1.800000 ^ 7 = 61.222003 1.800000 ^ 8 = 110.199606 1.800000 ^ 9 = 198.359290 1.800000 ^ 10 = 357.046723 1.900000 ^ 2 = 3.610000 1.900000 ^ 3 = 6.859000 1.900000 ^ 4 = 13.032100 1.900000 ^ 5 = 24.760990 1.900000 ^ 6 = 47.045881 1.900000 ^ 7 = 89.387174 1.900000 ^ 8 = 169.835630 1.900000 ^ 9 = 322.687698 1.900000 ^ 10 = 613.106626 2.000000 ^ 2 = 4.000000 2.000000 ^ 3 = 8.000000 2.000000 ^ 4 = 16.000000 2.000000 ^ 5 = 32.000000 2.000000 ^ 6 = 64.000000 2.000000 ^ 7 = 128.000000 2.000000 ^ 8 = 256.000000 2.000000 ^ 9 = 512.000000 2.000000 ^ 10 = 1024.000000
Functions5.pl
```#!/usr/bin/env perl
use utf8;
use Utils;
use strict;
use warnings;

sub factorial {
my (\$x) = @_;
if (\$x <= 1) {
return 1;
}
return \$x * factorial(\$x - 1);
}

MAIN:
{
binmode(STDOUT, ":utf8");
binmode(STDERR, ":utf8");
binmode(STDIN, ":utf8");
for (my \$x = 1; \$x < 10; ++\$x) {
print Utils::messageFormat("\{0:d\}! = \{1:d\}", \$x, factorial(\$x)), "\n";
}
}

```
Output
Lists6.pl
```#!/usr/bin/env perl
use utf8;
use Utils;
use strict;
use warnings;

sub reverseList {
my (\$x) = @_;
my \$y = [];
for (my \$i = scalar(@{\$x}) - 1; \$i >= 0; --\$i) {
push(@{\$y}, \$x->[\$i]);
}
return \$y;
}

MAIN:
{
my \$names = ["Fred", "Wilma", "Barney", "Betty"];

foreach my \$name (@\$names) {
print "Hello, ", \$name, "!\n";
}

\$names = reverseList(\$names);
foreach my \$name (@\$names) {
print "Hello, ", \$name, "!\n";
}

print Utils::listToString(\$names), "\n";
}

```
Output
\$ perl Lists6.pl Hello, Fred! Hello, Wilma! Hello, Barney! Hello, Betty! Hello, Betty! Hello, Barney! Hello, Wilma! Hello, Fred! [Betty, Barney, Wilma, Fred]
Maps3.pl
```#!/usr/bin/env perl
use utf8;
use Utils;
use strict;
use warnings;

sub convertKMtoMiles {
my (\$x) = @_;
my \$y = {};
foreach my \$planet (keys(%\$x)) {
\$y->{\$planet} = int(\$x->{\$planet} * 0.621371 + 0.5);
}
return \$y;
}

MAIN:
{
my \$planetDiametersInKM = {
"Mercury" => 4879,
"Venus" => 12103,
"Earth" => 12756,
"Mars" => 6794,
"Jupiter" => 142985,
"Saturn" => 120534,
"Uranus" => 51115,
"Neptune" => 49534,
"Pluto" => 2374,
"Ceres" => 946,
"Eris" => 2326,
"Makemake" => 1430
};

my \$planetDiametersInMiles = convertKMtoMiles(\$planetDiametersInKM);
foreach my \$planet (keys(%\$planetDiametersInMiles)) {
print \$planet, " has a diameter of ", \$planetDiametersInMiles->{\$planet}, " miles\n";
}
}

```
Output
\$ perl Maps3.pl Neptune has a diameter of 30779 miles Venus has a diameter of 7520 miles Makemake has a diameter of 889 miles Mars has a diameter of 4222 miles Jupiter has a diameter of 88847 miles Saturn has a diameter of 74896 miles Ceres has a diameter of 588 miles Earth has a diameter of 7926 miles Eris has a diameter of 1445 miles Uranus has a diameter of 31761 miles Pluto has a diameter of 1475 miles Mercury has a diameter of 3032 miles
Tuples3.pl
```#!/usr/bin/env perl
use utf8;
use Utils;
use strict;
use warnings;

sub swap {
my (\$x) = @_;
my \$y;
\$y = [\$x->[1], \$x->[0]];
return \$y;
}

MAIN:
{
my \$pair1 = ["Hello", 5];
my \$pair2;
\$pair2 = swap(\$pair1);
print Utils::tupleToString(\$pair1), " becomes ", Utils::tupleToString(\$pair2), "\n";
}

```
Output
\$ perl Tuples3.pl <Hello, 5> becomes <5, Hello>
perl

Questions

• {{Who's on first?}}
• {{Who's on second?}}
• {{Who's on third?}}

Projects

More ★'s indicate higher difficulty level.

References

• [[Perl Language Reference]]
• [[Comprehensive Perl Archive Network (CPAN)]]
• [[Beginning Perl]], Simon Cozens
• [[Perl Monks Tutorials]]