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
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.GeometryisGeometry.pm;My::TextisMy/Text.pm.Exporterwith@EXPORT_OKlets the caller import names it asks for; without it, callGeometry::area(...).- Core modules come with Perl:
List::Util,Scalar::Util,POSIX,Data::Dumper,Time::Local,Getopt::Long,Test::Moreand many more. - Modules from CPAN (
cpan,cpanm) are not available: runs have no network and nothing is installed during a run.