PerlHash

public final class PerlHash : PerlValue

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.

  • Creates an empty Perl hash.

    Declaration

    Swift

    public convenience init()
  • Creates an empty Perl hash.

    Declaration

    Swift

    public convenience init(perl: PerlInterpreter = .current)
  • Initializes a new Perl hash with elements of dictionary dict, recursively converting them to Perl scalars.

    Values can be simple scalars:

    let dict = ["one": 1, "two": 2, "three": 3]
    let hv = PerlHash(dict) // my %hv = (one => 1, two => 2, three => 3);
    

    More then that arrays, dictionaries, references and objects are also possible:

    let dict = ["odd": [1, 3], "even": [2, 4]]
    let hv = PerlHash(dict) // my %hv = (odd => [1, 3], even => [2, 4]);
    

    Declaration

    Swift

    public convenience init<T : PerlScalarConvertible>(_ dict: [String: T])

    Parameters

    dict

    a dictionary with String keys and values convertible to Perl scalars (conforming to PerlScalarConvertible).

  • Short form of init(dereferencing:).

    Declaration

    Swift

    public convenience init(_ ref: PerlScalar) throws
  • Returns the specified Perl global or package hash with the given name (so it won’t work on lexical variables). If the variable does not exist then nil is returned.

    Declaration

    Swift

    public convenience init?(get name: String, perl: PerlInterpreter = .current)
  • Returns the specified Perl global or package hash with the given name (so it won’t work on lexical variables). If the variable does not exist then it will be created.

    Declaration

    Swift

    public convenience init(getCreating name: String, perl: PerlInterpreter = .current)
  • A textual representation of the HV, suitable for debugging.

    Declaration

    Swift

    public override var debugDescription: String
  • Fetches the value associated with the given key.

    Declaration

    Swift

    public func fetch<T : PerlScalarConvertible>(_ key: String) throws -> T?

    Parameters

    key

    The key to find in the hash.

    Return Value

    The value associated with key if key is in the hash; otherwise, nil.

  • Stores the value in the hash for the given key.

    Declaration

    Swift

    public func store<T : PerlScalarConvertible>(key: String, value: T)

    Parameters

    key

    The key to associate with value.

    value

    The value to store in the hash.

  • Deletes the given key and its associated value from the hash.

    Declaration

    Swift

    public func delete<T : PerlScalarConvertible>(_ key: String) throws -> T?

    Parameters

    key

    The key to remove along with its associated value.

    Return Value

    The value that was removed, or nil if the key was not found in the hash.

  • Deletes the given key and its associated value from the hash.

    Declaration

    Swift

    public func delete(_ key: String)
  • Returns a boolean indicating whether the specified hash key exists.

    Declaration

    Swift

    public func exists(_ key: String) -> Bool
  • Fetches the value associated with the given key.

    Declaration

    Swift

    public func fetch<T : PerlScalarConvertible>(_ key: PerlScalar) throws -> T?

    Parameters

    key

    The key to find in the hash.

    Return Value

    The value associated with key if key is in the hash; otherwise, nil.

  • Stores the value in the hash for the given key.

    Declaration

    Swift

    public func store<T : PerlScalarConvertible>(key: PerlScalar, value: T)

    Parameters

    key

    The key to associate with value.

    value

    The value to store in the hash.

  • Deletes the given key and its associated value from the hash.

    Declaration

    Swift

    public func delete<T : PerlScalarConvertible>(_ key: PerlScalar) throws -> T?

    Parameters

    key

    The key to remove along with its associated value.

    Return Value

    The value that was removed, or nil if the key was not found in the hash.

  • Deletes the given key and its associated value from the hash.

    Declaration

    Swift

    public func delete(_ key: PerlScalar)
  • Returns a boolean indicating whether the specified hash key exists.

    Declaration

    Swift

    public func exists(_ key: PerlScalar) -> Bool
  • Frees the all the elements of a hash, leaving it empty.

    Declaration

    Swift

    public func clear()
  • Returns an iterator over the elements of this hash.

    PerlHash conforms to IteratorProtocol itself. So a returned value is always self. Behind the scenes it calls Perl macro hv_iterinit and prepares a starting point to traverse the hash table.

    Attention

    Only one iterator is possible at any time.

    See also

    Sequence

    Declaration

    Swift

    public func makeIterator() -> PerlHash

    Return Value

    self

  • Advances to the next element and returns it, or nil if no next element exists.

    Once nil has been returned, all subsequent calls return nil.

    See also

    IteratorProtocol

    Declaration

    Swift

    public func next() -> Element?
  • Accesses the value associated with the given key for reading and writing.

    This key-based subscript returns the value for the given key if the key is found in the hash, or nil if the key is not found.

    When you assign a value for a key and that key already exists, the hash overwrites the existing value. If the hash doesn’t contain the key, the key and value are added as a new key-value pair.

    If you assign nil as the value for the given key, the hash removes that key and its associated value.

    See also

    Dictionary

    Declaration

    Swift

    public subscript(key: Key) -> PerlScalar?

    Parameters

    key

    The key to find in the hash.

    Return Value

    The value associated with key if key is in the hash; otherwise, nil.

  • Accesses the value associated with the given key for reading and writing.

    This key-based subscript returns the value for the given key if the key is found in the hash, or nil if the key is not found.

    When you assign a value for a key and that key already exists, the hash overwrites the existing value. If the hash doesn’t contain the key, the key and value are added as a new key-value pair.

    If you assign nil as the value for the given key, the hash removes that key and its associated value.

    See also

    Dictionary

    Declaration

    Swift

    public subscript(key: PerlScalar) -> PerlScalar?

    Parameters

    key

    The key to find in the hash.

    Return Value

    The value associated with key if key is in the hash; otherwise, nil.

  • Creates a Perl hash from a Swift dictionary.

    Declaration

    Swift

    public convenience init(_ dict: [Key: Value])
  • Creates a Perl hash from a Swift array of key/value tuples.

    Declaration

    Swift

    public convenience init(_ elements: [(Key, Value)])
  • Creates a Perl hash initialized with a dictionary literal.

    Do not call this initializer directly. It is called by the compiler to handle dictionary literals. To use a dictionary literal as the initial value of a hash, enclose a comma-separated list of key-value pairs in square brackets. For example:

    let header: PerlHash = [
        "Content-Length": 320,
        "Content-Type": "application/json"
    ]
    

    See also

    ExpressibleByDictionaryLiteral

    Declaration

    Swift

    public convenience init(dictionaryLiteral elements: (Key, Value)...)

    Parameters

    elements

    The key-value pairs that will make up the new dictionary. Each key in elements must be unique.