Classes

The following classes are available globally.

  • Provides a safe wrapper for Perl array (AV). Performs reference counting on initialization and deinitialization.

    Cheat Sheet

    Array of strings

    my @list = ("one", "two", "three");
    
    let list: PerlArray = ["one", "two", "three"]
    

    Array of mixed type data (PSGI response)

    my @response = (200, ["Content-Type" => "application/json"], ["{}"]);
    
    let response: PerlArray = [200, ["Content-Type", "application/json"], ["{}"]]
    

    Accessing elements of the array

    my @list;
    $list[0] = 10
    push @list, 20;
    my $first = shift @list;
    my $second = $list[0]
    
    let list: PerlArray = []
    list[0] = 10
    list.append(20)
    let first = list.removeFirst()
    let second = list[0]
    
    See more

    Declaration

    Swift

    public final class PerlArray : PerlValue
  • Provides a safe wrapper for Perl subroutine (CV). Performs reference counting on initialization and deinitialization.

    Cheat Sheet

    Creation of an anonymous subroutine

    my $summer = sub {
        my ($lv, $rv) = @_;
        return $lv + $rv;
    }
    
    let summer = PerlSub {
        (lv: Int, rv: Int) -> Int in
        return lv + rv
    }
    

    In fact, these examples are not fully equal. The Perl version returns a SV pointing to a CV, whereas the Swift version returns just a CV.

    Creation of a named subroutine

    sub strlen {
        return length $_[0];
    }
    
    PerlSub(name: "strlen") { (s: String) in
        return s.characters.count
    }
    

    Calling a subroutine

    my $sum = $summer->(10, 20);
    
    let sum = summer.call(10, 20)
    
    See more

    Declaration

    Swift

    public final class PerlSub : PerlValue
  • Provides a safe wrapper for Perl objects (blessed references). Performs reference counting on initialization and deinitialization.

    Any Perl object of unregistered type will be imported to Swift as an instance of this class. To provide clean API to your Perl object implement class derived from PerlObject, make it conforming to PerlNamedClass and supply it with methods and calculated attributes providing access to Perl methods of your object. Use register method on startup to enable automatical conversion of Perl objects of class perlClassName to instances of your Swift class.

    For example:

    final class URI : PerlObject, PerlNamedClass {
        static let perlClassName = "URI"
    
        convenience init(_ str: String) throws {
            try self.init(method: "new", args: [str])
        }
    
        convenience init(_ str: String, scheme: String) throws {
            try self.init(method: "new", args: [str, scheme])
        }
    
        convenience init(copyOf uri: URI) {
            try! self.init(uri.call(method: "clone") as PerlScalar)
        }
    
        var scheme: String? { return try! call(method: "scheme") }
        func scheme(_ scheme: String) throws -> String? { return try call(method: "scheme", scheme) }
    
        var path: String {
            get { return try! call(method: "path") }
            set { try! call(method: "path", newValue) as Void }
        }
    
        var asString: String { return try! call(method: "as_string") }
    
        func abs(base: String) -> String { return try! call(method: "abs", base) }
        func rel(base: String) -> String { return try! call(method: "rel", base) }
    
        var secure: Bool { return try! call(method: "secure") }
    }
    
    See more

    Declaration

    Swift

    open class PerlObject : PerlValue, PerlScalarConvertible
  • Provides a safe wrapper for Perl hash (HV). Performs reference counting on initialization and deinitialization.

    Cheat Sheet

    Creating of a hash

    my %hash = (
        id => 42,
        name => "Иван",
        aliases => ["Ваня", "John"],
    );
    
    let hash: PerlHash = [
        "id": 42,
        "name": "Иван",
        "aliases": ["Ваня", "John"],
    ]
    

    Accessing a hash

    $hash{age} = 10;
    my $age = $hash{age};
    delete $hash{age};
    my $has_age = exists $hash{age};
    $hash{age} = undef;
    
    hash["age"] = 10
    let age = hash["age"] ?? PerlScalar()
    hash["age"] = nil
    let hasAge = hash["age"] != nil
    hash["age"] = PerlScalar()
    

    The difference between Perl and Swift hash element access APIs is the result of Swiftification. It was done to make subscript behavior match behavior of subscripts in Dictionary. So, when a key does not exist subscript returns nil not an undefined SV as a Perl programmer could expect.

    See more

    Declaration

    Swift

    public final class PerlHash : PerlValue
  • Provides a safe wrapper for Perl scalar (SV). Performs reference counting on initialization and deinitialization.

    Can contain any scalar SV with SvTYPE(sv) < SVt_PVAV such as: undefined values, integers (IV), numbers (NV), strings (PV), references (RV), objects and others. Objects as exception have their own type PerlObject which provides more specific methods to work with them. Nevertheless objects are compatible with and can be represented as PerlScalar.

    Cheat Sheet

    Creation of various scalars

    my $int = 10;
    my $str = "Строченька";
    my $intref = \10;
    my $arrayref = [200, "OK"];
    my $hashref = { type => "string", value => 10 };
    
    let int: PerlScalar = 10
    let str: PerlScalar = "Строченька"
    let intref = PerlScalar(referenceTo: PerlScalar(10))
    let arrayref: PerlScalar = [200, "OK"];
    let hashref: PerlScalar = ["type": "string", "value": 10]
    
    See more

    Declaration

    Swift

    public final class PerlScalar : PerlValue
  • Provides a safe wrapper for any SV, which can contain any Perl value, not only scalars. Performs reference counting on initialization and deinitialization.

    See more

    Declaration

    Swift

    open class PerlValue : AnyPerl, CustomDebugStringConvertible