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
SVcontaining an undefined value.Declaration
Swift
public convenience init() -
Creates a
SVcontaining an undefined value.Declaration
Swift
public convenience init(perl: PerlInterpreter = .current) -
Creates a
SVcontainig av.Declaration
Swift
public convenience init<T : PerlScalarConvertible>(_ v: T, perl: PerlInterpreter = .current) -
Semantics of a Perl string data.
See moreDeclaration
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
RVpointing to aAVwhich containsSVs with elements of anarray.Declaration
Swift
public convenience init<T : PerlScalarConvertible>(_ array: [T], perl: PerlInterpreter = .current) -
Creates a
RVpointing to aHVwhich containsSVs with elements of adict.Declaration
Swift
public convenience init<T : PerlScalarConvertible>(_ dict: [String: T], perl: PerlInterpreter = .current) -
Creates a
SVcontainig an unwrapped value of avifv != nilor anundefin 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
nilis 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
SVis defined.Declaration
Swift
public var defined: Bool -
A boolean value indicating whether the
SVcontains an integer (signed or unsigned).Declaration
Swift
public var isInteger: Bool -
A boolean value indicating whether the
SVcontains a double.Declaration
Swift
public var isDouble: Bool -
A boolean value indicating whether the
SVcontains a character string.Declaration
Swift
public var isString: Bool -
A boolean value indicating whether the
SVis a reference.Declaration
Swift
public var isReference: Bool -
A boolean value indicating whether the
SVis an object.Declaration
Swift
public var isObject: Bool -
Dereferences the
SVif it is a reference. Returnsnilif not.Declaration
Swift
public var referent: AnyPerl? -
Calls the closure with
UnsafeRawBufferPointerto 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
PerlScalarinstance is defined, passingselfas a parameter.Use the
mapmethod with a closure that returns a nonoptional value.Declaration
Swift
public func map<R>(_ transform: (PerlScalar) throws -> R) rethrows -> R?Parameters
transformA 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
PerlScalarinstance is defined, passingselfas a parameter.Use the
flatMapmethod with a closure that returns an optional value.Declaration
Swift
public func flatMap<R>(_ transform: (PerlScalar) throws -> R?) rethrows -> R?Parameters
transformA 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
selfwhen 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
valueinto the destination SVself. 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
NULcharacters) intoself. Does not handle ‘set’ magic.Declaration
Swift
public func set(_ value: String) -
Copies bytes or characters from
valueintoself. 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
PerlScalarinstance with anilliteral. For example:let sv: PerlScalar = nilDeclaration
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
PerlScalarinstance by using one of the boolean literalstrueandfalse.let sv: PerlScalar = trueDeclaration
Swift
public convenience init(booleanLiteral value: Bool)Parameters
valueThe 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
PerlScalarinstance by using an integer literal. Instead, create a new value by using a literal:let x: PerlScalar = 100Declaration
Swift
public convenience init(integerLiteral value: Int)Parameters
valueThe 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
PerlScalarinstance by using a floating-point literal. Instead, create a new value by using a literal:let x: PerlScalar = 1.1Declaration
Swift
public convenience init(floatLiteral value: Double)Parameters
valueThe 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
PerlScalarusing 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
PerlScalarusing 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
PerlScalarusing a string literal. For example:let sv: PerlScalar = "My World"This assignment to the
svcalls 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
PerlScalarby 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
elementsA 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
elementsThe key-value pairs that will make up the new dictionary. Each key in
elementsmust be unique.
View on GitHub
PerlScalar Class Reference