PerlOnJava includes many common CPAN modules and supports adding additional pure Perl modules to your projects. It also includes a custom ExtUtils::MakeMaker that allows you to install pure Perl CPAN modules directly without needing make or native compilation.
For pure Perl modules with a standard Makefile.PL:
# Download and extract the module
curl -O https://cpan.metacpan.org/authors/id/X/XX/AUTHOR/Module-Name-1.00.tar.gz
tar xzf Module-Name-1.00.tar.gz
cd Module-Name-1.00
# Install with jperl (no make needed!)
jperl Makefile.PL
# The module is now installed to ~/.perlonjava/lib/
# and automatically available in @INCTo check if a module is available in PerlOnJava:
./jperl -e 'use Module::Name; print "Available\n"'PerlOnJava includes:
strict,warnings,utf8,featureCarp,Config,Cwd,ExporterFile::Spec,File::Basename,File::Copy,File::Find,File::Path,File::TempIO::File,IO::Handle,FileHandle,DirHandleGetopt::Long,Getopt::StdSys::Hostname- System hostnameSymbol- Symbol manipulation
IPC::Open2,IPC::Open3- Bi-directional process communication
ExtUtils::MakeMaker- Module installation (PerlOnJava version)
JSON- JSON encoding/decodingYAML- YAML parsingTOML- TOML parsingText::CSV- CSV parsingData::Dumper- Data structure dumpingStorable- Data serialization
Digest::MD5,Digest::SHA- Hash algorithmsMIME::Base64,MIME::QuotedPrint- EncodingEncode- Character encoding
HTTP::Tiny- HTTP clientSocket- Low-level socket supportIO::Socket::INET,IO::Socket::UNIX- TCP/IP and Unix socketsNet::FTP- FTP clientNet::SMTP- SMTP clientNet::POP3- POP3 clientNet::NNTP- NNTP client
Archive::Tar- Tar file handlingArchive::Zip- Zip file handlingCompress::Zlib- CompressionIO::Zlib- Compressed I/O
DBI- Database interface (with JDBC backend)
Test::More,Test::Simple,Test::Builder
Time::HiRes- High-resolution timeTime::Piece- Time manipulationPOSIX- POSIX functions including strftime
If you need a CPAN module that's not included, you can often add pure Perl modules directly.
PerlOnJava includes a custom ExtUtils::MakeMaker that installs pure Perl modules directly:
# Download and extract
tar xzf Some-Module-1.00.tar.gz
cd Some-Module-1.00
# Run Makefile.PL with jperl
jperl Makefile.PLWhat happens:
- For pure Perl modules:
.pmfiles are copied to~/.perlonjava/lib/ - For XS modules: You'll see guidance on porting options
Customizing the install location:
# Install to a specific directory
PERLONJAVA_LIB=/path/to/my/libs jperl Makefile.PLThe default ~/.perlonjava/lib/ directory is automatically included in @INC, so installed modules work immediately.
Create a lib directory in your project and add modules there:
mkdir -p myproject/lib
cp /path/to/Some/Module.pm myproject/lib/Some/Module.pmRun with:
./jperl -Imyproject/lib myscript.plexport PERL5LIB=/path/to/your/modules
./jperl myscript.plTo check if a CPAN module is pure Perl:
- Visit https://metacpan.org/pod/Module::Name
- Look at the source files
- If there's only
.pmfiles (no.xsor.cfiles), it's pure Perl
# Download a module from CPAN
curl -O https://cpan.metacpan.org/authors/id/X/XX/XXXX/Module-Name-1.00.tar.gz
# Extract
tar xzf Module-Name-1.00.tar.gz
# Copy the lib directory
cp -r Module-Name-1.00/lib/* myproject/lib/Some CPAN modules have XS (C/C++) components that won't work directly. PerlOnJava's ExtUtils::MakeMaker automatically detects XS modules and provides guidance:
XS MODULE DETECTED: Some::XS::Module
============================================================
This module contains XS/C code that cannot be used directly.
PerlOnJava compiles to JVM bytecode, not native code.
XS/C files found:
- Module.xs
Options:
1. Check if PerlOnJava already has a Java implementation
2. Look for a pure Perl alternative module on CPAN
3. Port the XS code to Java
For XS modules, your options are:
- Check if PerlOnJava has a Java port - Many common XS modules have Java implementations
- Look for pure Perl alternatives - e.g., use
JSONinstead ofJSON::XS - Request a port - Open an issue at the PerlOnJava repository
| XS Module | Java Alternative in PerlOnJava |
|---|---|
| JSON::XS | JSON (built-in) |
| Compress::Raw::Zlib | Compress::Zlib (built-in) |
| Digest::MD5 (XS part) | Digest::MD5 (Java implementation) |
| DBI (XS part) | DBI (JDBC backend) |
| Time::HiRes (XS part) | Time::HiRes (Java implementation) |
use Archive::Zip qw(:ERROR_CODES);
my $zip = Archive::Zip->new();
my $status = $zip->read('archive.zip');
die "Read failed" unless $status == AZ_OK;
for my $member ($zip->members()) {
print $member->fileName(), "\n";
}use Archive::Zip qw(:ERROR_CODES);
my $zip = Archive::Zip->new();
$zip->addFile('document.txt');
$zip->addString("Hello!", 'hello.txt');
my $status = $zip->writeToFileNamed('output.zip');use Archive::Tar;
# Read
my $tar = Archive::Tar->new('archive.tar.gz');
my @files = $tar->list_files();
$tar->extract();
# Create
my $tar = Archive::Tar->new();
$tar->add_files('file1.txt', 'file2.txt');
$tar->write('output.tar.gz', COMPRESS_GZIP);use HTTP::Tiny;
my $http = HTTP::Tiny->new();
my $response = $http->get('https://api.github.com/repos/perl/perl5');
if ($response->{success}) {
print $response->{content};
}Here's a helper script to download and extract a CPAN module:
#!/usr/bin/env jperl
use strict;
use warnings;
use HTTP::Tiny;
use Archive::Tar;
use File::Temp qw(tempfile);
use File::Path qw(make_path);
my $module = shift or die "Usage: $0 Module::Name\n";
my $dest = shift || 'lib';
# Convert module name to path
(my $path = $module) =~ s/::/-/g;
# Query MetaCPAN for download URL
my $http = HTTP::Tiny->new();
my $resp = $http->get("https://fastapi.metacpan.org/v1/download_url/$module");
if (!$resp->{success}) {
die "Could not find $module on CPAN\n";
}
# Parse JSON response
use JSON;
my $data = decode_json($resp->{content});
my $url = $data->{download_url};
print "Downloading $url\n";
my $tarball = $http->get($url);
if (!$tarball->{success}) {
die "Download failed\n";
}
# Save to temp file
my ($fh, $filename) = tempfile(SUFFIX => '.tar.gz');
print $fh $tarball->{content};
close $fh;
# Extract
my $tar = Archive::Tar->new($filename);
my @files = $tar->list_files();
make_path($dest);
for my $file (@files) {
next unless $file =~ m{/lib/(.+\.pm)$};
my $target = "$dest/$1";
my $dir = $target;
$dir =~ s{/[^/]+$}{};
make_path($dir);
my $content = $tar->get_content($file);
open my $out, '>', $target or die "Cannot write $target: $!";
print $out $content;
close $out;
print "Installed $target\n";
}
unlink $filename;
print "Done!\n";The module is not installed. Check:
- Is it a pure Perl module? XS modules won't work directly.
- Is the module in your lib path? Use
-I/path/to/lib
This means the module requires an XS implementation that hasn't been ported to Java. Check if there's a pure Perl alternative.
Some modules may load but have unsupported features:
- Check if the module uses XS functions internally
- Some Perl built-in functions may not be fully implemented
- PerlOnJava Repository: https://github.com/fglock/PerlOnJava
- Issues: Report missing modules or compatibility problems
- Feature Matrix: See
docs/reference/feature-matrix.mdfor supported features