BSFPerl Home
BSFPerl Tutorial
SourceForge Project Home
Design Decisions
Submit a bug Logo Donate to this project

BSFPerl Tutorial

This is a very high-level overview of the functionality that BSFPerl provides. The current implementation details are a bit counter-intuitive and are not documented particularly well at the moment.

BSFPerl overloads Perl's import function so that you can also import Java classes into your script. Because '.' is not a valid identifier in Perl, you need to use Perl's usual scoping operator ('::') to separate the components of the package. For example, to refer to java.util.Random you would use java::util::Random.

When import is invoked on a Java class, it will create a Perl package named with the class part of the import (e.g., Random). Any subsequent method calls that are attempted on this package will be forwarded on to Java, which will search for a static method of the appropriate signature.

BSFPerl treats a method call to new differently; instead of looking for a static method with that name, it looks for a constructor that takes the appropriate arguments. If found, the constructor is invoked and a proxy to the constructed object is returned.

Method calls on proxy objects will also be forwarded to Java. Various data type conversions will be performed both on the input arguments as well as the return value.

Perl Java
SCALAR (numeric* integer) java.lang.Long
SCALAR (numeric* floating-point) java.lang.Double
SCALAR (non-numeric) java.lang.String
ARRAY java.util.ArrayList
HASH java.util.HashMap

Here's an example that covers all of this functionality:

    import javax::swing::JFrame;
    import javax::swing::JButton;
    import java::awt::BorderLayout;
    my $f = new JFrame("test");
    my $b = new JButton("Go");
    $f->getContentPane->setLayout(new BorderLayout());

This example will create a JFrame and display it to the user. Unfortunately, there is currently no way to implement Java interfaces from BSFPerl. This means that you cannot register an ActionListener from Perl. In future versions of BSFPerl, this will be changed so that an anonymous subroutine can be passed in place of the interface or bound to specific methods in the interface.

Note: To distinguish numeric from non-numeric scalars, we actually cheat a little bit. Although it's not supposed to expose it, Perl does differentiate between values that it thinks are strings and those that it thinks are numeric. It will freely convert between them when necessary, but it also uses this information in a few cases. In particular, the bitwise XOR operator behaves differently depending on whether its arguments are numeric (meaning they were parsed as unquoted numeric literals, or they were the result of some arithmetic calculation) or non-numeric (meaning that they were quoted literals, could not be parsed as a numeric value, or were the result of a non-arithmetic calculation). To differentiate integer vs. floating-point numerics we simply use a regular expression of BSFPerl's parsing logic.