Modules and several files

Put subs in a .pm module, load it with use lib and use, and use core modules such as List::Util.

Runs in Zerethon IDE · Perl 5.40 Official Perl documentation ↗
main.pl
use v5.40;
use lib ".";
use Geometry qw(area);
use List::Util qw(sum max);

say area(3, 4);
say sum(1, 2, 3), " ", max(4, 9, 2);
Geometry.pm
package Geometry;
use v5.40;
use Exporter 'import';
our @EXPORT_OK = ('area');

sub area ($width, $height) {
    return $width * $height;
}

Output

12
6 9
Two files: main.pl (the entry, run by Run) and the module Geometry.pm next to it.
  • use lib "."; adds the project root to the places Perl looks for modules. Geometry is Geometry.pm; My::Text is My/Text.pm.
  • Exporter with @EXPORT_OK lets the caller import names it asks for; without it, call Geometry::area(...).
  • Core modules come with Perl: List::Util, Scalar::Util, POSIX, Data::Dumper, Time::Local, Getopt::Long, Test::More and many more.
  • Modules from CPAN (cpan, cpanm) are not available: runs have no network and nothing is installed during a run.