PerlScalar

public final class PerlScalar : 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]
  • Creates a SV containing an undefined value.

    Declaration

    Swift

    public convenience init()
  • Creates a SV containing an undefined value.

    Declaration

    Swift

    public convenience init(perl: PerlInterpreter = .current)
  • Creates a SV containig a v.

    Declaration

    Swift

    public convenience init<T : PerlScalarConvertible>(_ v: T, perl: PerlInterpreter = .current)
  • Semantics of a Perl string data.

    See more

    Declaration

    Swift

    public enum StringUnits
  • Creates a Perl string containing a copy of bytes or characters from v.

    Declaration

    Swift

    public convenience init(_ v: UnsafeRawBufferPointer, containing: StringUnits = .bytes, perl: PerlInterpreter = .current)
  • Creates a new SV which is an exact duplicate of the original SV.

    Declaration

    Swift

    public convenience init(copy scalar: PerlScalar)
  • Creates a new reference pointing to value.

    Declaration

    Swift

    public convenience init<T : PerlValue>(referenceTo value: T)
  • Short form of init(referenceTo:).

    Declaration

    Swift

    public convenience init(_ value: PerlArray)
  • Short form of init(referenceTo:).

    Declaration

    Swift

    public convenience init(_ value: PerlHash)
  • Short form of init(referenceTo:).

    Declaration

    Swift

    public convenience init(_ value: PerlSub)
  • Creates a RV pointing to a AV which contains SVs with elements of an array.

    Declaration

    Swift

    public convenience init<T : PerlScalarConvertible>(_ array: [T], perl: PerlInterpreter = .current)
  • Creates a RV pointing to a HV which contains SVs with elements of a dict.

    Declaration

    Swift

    public convenience init<T : PerlScalarConvertible>(_ dict: [String: T], perl: PerlInterpreter = .current)
  • Creates a SV containig an unwrapped value of a v if v != nil or an undef in other case.

    Declaration

    Swift

    public convenience init<T : PerlScalarConvertible>(_ v: T?, perl: PerlInterpreter = .current)
  • Returns the specified Perl global or package scalar 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 scalar 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 boolean value indicating whether the SV is defined.

    Declaration

    Swift

    public var defined: Bool
  • A boolean value indicating whether the SV contains an integer (signed or unsigned).

    Declaration

    Swift

    public var isInteger: Bool
  • A boolean value indicating whether the SV contains a double.

    Declaration

    Swift

    public var isDouble: Bool
  • A boolean value indicating whether the SV contains a character string.

    Declaration

    Swift

    public var isString: Bool
  • A boolean value indicating whether the SV is a reference.

    Declaration

    Swift

    public var isReference: Bool
  • A boolean value indicating whether the SV is an object.

    Declaration

    Swift

    public var isObject: Bool
  • Dereferences the SV if it is a reference. Returns nil if not.

    Declaration

    Swift

    public var referent: AnyPerl?
  • Calls the closure with UnsafeRawBufferPointer to the string in the SV, or a stringified form of the SV if the SV does not contain a string.

    Declaration

    Swift

    public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R
  • Evaluates the given closure when this PerlScalar instance is defined, passing self as a parameter.

    Use the map method with a closure that returns a nonoptional value.

    Declaration

    Swift

    public func map<R>(_ transform: (PerlScalar) throws -> R) rethrows -> R?

    Parameters

    transform

    A closure that takes self.

    Return Value

    The result of the given closure. If this instance is undefined, returns nil.

  • Evaluates the given closure when this PerlScalar instance is defined, passing self as a parameter.

    Use the flatMap method with a closure that returns an optional value.

    Declaration

    Swift

    public func flatMap<R>(_ transform: (PerlScalar) throws -> R?) rethrows -> R?

    Parameters

    transform

    A closure that takes self.

    Return Value

    The result of the given closure. If this instance is undefined, returns nil.

  • Performs an undef-coalescing operation, returning self when it is defined, or a default value.

    Declaration

    Swift

    public static func ??(scalar: PerlScalar, defaultValue: @autoclosure () throws -> PerlScalar) rethrows -> PerlScalar
  • Copies the contents of the source SV value into the destination SV self. Does not handle ‘set’ magic on destination SV. Calls ‘get’ magic on source SV. Loosely speaking, it performs a copy-by-value, obliterating any previous content of the destination.

    Declaration

    Swift

    public func set(_ value: PerlScalar)
  • Copies a boolean into self. Does not handle ‘set’ magic.

    Declaration

    Swift

    public func set(_ value: Bool)
  • Copies a signed integer into self, upgrading first if necessary. Does not handle ‘set’ magic.

    Declaration

    Swift

    public func set(_ value: Int)
  • Copies an unsigned integer into self, upgrading first if necessary. Does not handle ‘set’ magic.

    Declaration

    Swift

    public func set(_ value: UInt)
  • Copies a double into self, upgrading first if necessary. Does not handle ‘set’ magic.

    Declaration

    Swift

    public func set(_ value: Double)
  • Copies a string (possibly containing embedded NUL characters) into self. Does not handle ‘set’ magic.

    Declaration

    Swift

    public func set(_ value: String)
  • Copies bytes or characters from value into self. Does not handle ‘set’ magic.

    Declaration

    Swift

    public func set(_ value: UnsafeRawBufferPointer, containing: StringUnits = .bytes)
  • A textual representation of the SV, suitable for debugging.

    Declaration

    Swift

    public override var debugDescription: String
  • The hash value of the stringified form of the scalar.

    Hash values are not guaranteed to be equal across different executions of your program. Do not save hash values to use during a future execution.

    Declaration

    Swift

    public var hashValue: Int
  • Returns a Boolean value indicating whether two scalars stringify to identical strings.

    Declaration

    Swift

    public static func == (lhs: PerlScalar, rhs: PerlScalar) -> Bool
  • Creates an instance which contains undef.

    Do not call this initializer directly. It is used by the compiler when you initialize an PerlScalar instance with a nil literal. For example:

    let sv: PerlScalar = nil
    

    Declaration

    Swift

    public convenience init(nilLiteral: ())
  • Creates an instance initialized to the specified boolean literal.

    Do not call this initializer directly. It is used by the compiler when you use a boolean literal. Instead, create a new PerlScalar instance by using one of the boolean literals true and false.

    let sv: PerlScalar = true
    

    Declaration

    Swift

    public convenience init(booleanLiteral value: Bool)

    Parameters

    value

    The value of the new instance.

  • Creates an instance from the given integer literal.

    Do not call this initializer directly. It is used by the compiler when you create a new PerlScalar instance by using an integer literal. Instead, create a new value by using a literal:

    let x: PerlScalar = 100
    

    Declaration

    Swift

    public convenience init(integerLiteral value: Int)

    Parameters

    value

    The new value.

  • Creates an instance from the given floating-point literal.

    Do not call this initializer directly. It is used by the compiler when you create a new PerlScalar instance by using a floating-point literal. Instead, create a new value by using a literal:

    let x: PerlScalar = 1.1
    

    Declaration

    Swift

    public convenience init(floatLiteral value: Double)

    Parameters

    value

    The new value.

  • Creates an instance initialized to the given Unicode scalar value.

    Don’t call this initializer directly. It may be used by the compiler when you initialize a PerlScalar using a string literal that contains a single Unicode scalar value.

    Declaration

    Swift

    public convenience init(unicodeScalarLiteral value: String)
  • Creates an instance initialized to the given extended grapheme cluster literal.

    Don’t call this initializer directly. It may be used by the compiler when you initialize a PerlScalar using a string literal containing a single extended grapheme cluster.

    Declaration

    Swift

    public convenience init(extendedGraphemeClusterLiteral value: String)
  • Creates an instance initialized to the given string value.

    Don’t call this initializer directly. It is used by the compiler when you initialize a PerlScalar using a string literal. For example:

    let sv: PerlScalar = "My World"
    

    This assignment to the sv calls this string literal initializer behind the scenes.

    Declaration

    Swift

    public convenience init(stringLiteral value: String)
  • Creates a reference to array from the given array literal.

    Do not call this initializer directly. It is used by the compiler when you use an array literal. Instead, create a new PerlScalar by using an array literal as its value. To do this, enclose a comma-separated list of values in square brackets. For example:

    let mix: PerlScalar = [nil, 100, "use perl or die"]
    

    Declaration

    Swift

    public convenience init (arrayLiteral elements: PerlScalar...)

    Parameters

    elements

    A variadic list of elements of the new array.

  • Creates a reference to 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 PerlScalar, enclose a comma-separated list of key-value pairs in square brackets. For example:

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

    Declaration

    Swift

    public convenience init(dictionaryLiteral elements: (String, PerlScalar)...)

    Parameters

    elements

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