perljava - PerlOnJava Portability Guide
This document provides guidelines for writing Perl scripts that are portable when compiled and executed using PerlOnJava, a Perl compiler that translates Perl code into Java bytecode for execution on the Java Virtual Machine (JVM).
PerlOnJava allows Perl scripts to be integrated into Java environments by compiling them into Java bytecode. This document outlines considerations and best practices for ensuring that your Perl scripts are portable and compatible with the PerlOnJava compiler.
PerlOnJava handles newlines in a manner consistent with the JVM. Ensure that your scripts account for newline differences across platforms by using Perl's built-in functions like chomp
and chop
. Be aware that newline handling may vary between Unix and Windows environments.
PerlOnJava supports basic file operations such as
open
,readline
, andclose
. However, some file test operators like-R
,-W
, and-X
are not implemented due to JVM limitations.Use platform-independent file paths and avoid relying on case sensitivity, as file systems may differ between environments.
The
stat
andlstat
functions are implemented, but some fields may returnundef
due to JVM constraints.
PerlOnJava supports executing Perl code dynamically using the
eval
function.External command execution should be handled with care, considering the JVM's limitations and the lack of direct system call support. Use Java's process execution capabilities for more complex needs.
PerlOnJava supports Unicode. Non-Unicode strings are not fully supported, so ensure your scripts handle character encoding appropriately.
Use Perl's
Encode
module where necessary to manage character encodings.
PerlOnJava does not support low-level socket functions due to JVM constraints.
High-level networking operations will be implemented using Java's native capabilities.
Time-related functions like
time
,gmtime
, andlocaltime
are implemented. However, be aware of potential differences in time zone handling across platforms.The
sleep
function is implemented and supports fractional seconds.
PerlOnJava supports a subset of Perl modules and pragmas. Refer to the FEATURE_MATRIX.md for details on supported modules.
Avoid using XS modules or CPAN libraries that rely on C code, as they are not compatible with the JVM.
The
strict
pragma is always enabled, andno strict
is ignored. Plan your code accordingly.
Basic regex operations are supported, including
qr//
,m//
, ands///
. However, some Perl-specific features are missing.Java's regex engine limitations may affect regex behavior, particularly with named capture groups. Features like
/xx
and/ee
are not supported.
PerlOnJava supports scalar operations, including
my
,our
, andlocal
declarations. Tied scalars are not supported.Basic array and hash operations are implemented, including
push
,pop
,keys
, andvalues
. Tied arrays and hashes are not supported.Autovivification is supported, but ensure your code does not rely on Perl-specific behaviors not implemented in PerlOnJava.
Named and anonymous subroutines are supported, including closures. Subroutine signatures and attributes are not implemented.
The
CORE
namespace is available, butCORE::GLOBAL
is not implemented.
Object-oriented features such as
bless
,ref
, and@ISA
are supported. Multiple inheritance with C3 method resolution is implemented.The
AUTOLOAD
mechanism is available, but ensure compatibility with JVM constraints.
Conditional statements like
if
,unless
, and loops likefor
,while
are supported.Operators such as
defined-or
(//
), string concatenation (.
), and logical operators are implemented.The
goto
operator is partially supported;goto LABEL
andgoto EXPR
are not implemented.
In Perl, the DESTROY
method is used to clean up an object before it is garbage collected. This method is typically used to release resources such as file handles, database connections, or other system resources that the object may hold.
PerlOnJava compiles Perl code into Java bytecode, which runs on the JVM. The JVM uses a garbage collection mechanism that differs from Perl's reference counting. This difference can lead to several challenges:
Timing of Object Destruction: In Perl, objects are destroyed immediately when their reference count drops to zero. In contrast, the JVM's garbage collector determines when to reclaim memory, which may not coincide with the exact moment an object becomes unreachable. This can delay the execution of the
DESTROY
method, potentially leading to resource leaks if resources are not released promptly.Resource Management: Since the JVM does not guarantee when or if the
DESTROY
method will be called, relying on it for critical resource management can be problematic. Resources such as file handles or network connections should be explicitly managed to ensure they are released promptly.Compatibility with Perl Modules: Some Perl modules rely on the
DESTROY
method for cleanup. When using such modules with PerlOnJava, be aware that their behavior might differ due to the JVM's garbage collection. Modules likeSelectSaver
andFile::Temp
may not function as expected because they depend on timely destruction.
To mitigate issues related to object destruction in PerlOnJava, consider the following best practices:
Explicit Resource Management: Avoid relying solely on the
DESTROY
method for resource cleanup. Instead, use explicit resource management techniques, such as closing file handles or database connections in afinally
block.Test and Validate: Thoroughly test your Perl scripts in the PerlOnJava environment to identify and address any issues related to object destruction and resource management.
PerlOnJava is designed to run on any platform that supports the JVM. However, be mindful of platform-specific behaviors and test your scripts across different environments to ensure compatibility.
JVM-specific limitations may affect certain Perl features, particularly those relying on low-level system interactions.
For more details on the PerlOnJava project, refer to the README.md and FEATURE_MATRIX.md files in the project repository.
Flavio S. Glock
This document is licensed under the same terms as the Perl Artistic License 2.0.