Skip to content

Commit 7eb6c35

Browse files
Reini UrbanReini Urban
authored andcommitted
perldata.pod: add const and coretypes
New paragraph "Constant and typed lexical variables"
1 parent 6331566 commit 7eb6c35

File tree

1 file changed

+88
-11
lines changed

1 file changed

+88
-11
lines changed

pod/perldata.pod

Lines changed: 88 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,17 @@ conversion from one form to another is transparent. Although a
167167
scalar may not directly hold multiple values, it may contain a
168168
reference to an array or hash which in turn contains multiple values.
169169

170-
Scalars aren't necessarily one thing or another. There's no place
171-
to declare a scalar variable to be of type "string", type "number",
172-
type "reference", or anything else. Because of the automatic
173-
conversion of scalars, operations that return scalars don't need
174-
to care (and in fact, cannot care) whether their caller is looking
175-
for a string, a number, or a reference. Perl is a contextually
176-
polymorphic language whose scalars can be strings, numbers, or
177-
references (which includes objects). Although strings and numbers
178-
are considered pretty much the same thing for nearly all purposes,
179-
references are strongly-typed, uncastable pointers with builtin
180-
reference-counting and destructor invocation.
170+
Scalars aren't necessarily one thing or another. Unless using
171+
L<coretypes> there's no place to declare a scalar variable to be of type
172+
"string", type "number", type "reference", or anything else. Because
173+
of the automatic conversion of scalars, operations that return scalars
174+
don't need to care (and in fact, cannot care) whether their caller is
175+
looking for a string, a number, or a reference. Perl is a
176+
contextually polymorphic language whose scalars can be strings,
177+
numbers, or references (which includes objects). Although strings and
178+
numbers are considered pretty much the same thing for nearly all
179+
purposes, references are strongly-typed, uncastable pointers with
180+
builtin reference-counting and destructor invocation.
181181

182182
A scalar value is interpreted as FALSE in the Boolean sense
183183
if it is undefined, the null string or the number 0 (or its
@@ -363,6 +363,83 @@ anything more complicated in the subscript will be interpreted as an
363363
expression. This means for example that C<$version{2.0}++> is
364364
equivalent to C<$version{2}++>, not to C<$version{'2.0'}++>.
365365

366+
=head3 Constant and typed lexical variables
367+
X<const> X<type>
368+
369+
You can declare lexical my, our or state variables with the
370+
type-qualifier C<const>. const is only available in 5.18 or higher,
371+
and only with a C<use v5.18> or C<use feature "const">
372+
declaration.
373+
374+
my const $i = 1;
375+
my const @i = (1);
376+
my const %i = ('ok' => 1);
377+
378+
You can also declare the type of a lexical variable with an existing
379+
packagename:
380+
381+
package Int;
382+
my Int $i;
383+
384+
my const Int $i = 0;
385+
386+
Lexical const variables cannot be modified, and thus better optimized
387+
internally at compile-time. This is similar to the L<Readonly>
388+
module, just not backwards compatible and const can throw errors
389+
earlier, at compile-time.
390+
391+
The type information can be used by external modules to implement type
392+
checks and type optimizations, such as using more efficient
393+
implementations and short-cut method calls. See
394+
L<perltypes/"Compile-time type optimizations">.
395+
396+
X<coretypes>
397+
With C<use coretypes> you can declare the type of a lexical variable
398+
as C<int>, C<double> or C<string>. C<double> uses the internal
399+
floating point represention, which might be better or worse than IEEE
400+
doubles.
401+
402+
Such native typed variables can not be magic, i.e. not tied, and type
403+
violations such as stringification of int or double, setting to undef
404+
or more will throw compile-time errors.
405+
406+
my int $j;
407+
my const int $i = 0;
408+
my int @a;
409+
my const int @b = (0..9);
410+
my string %h;
411+
my const string %h
412+
= ('ok' => '1',
413+
'bad' => '2');
414+
415+
Array keys are always of type int, so the array type declaration specifies
416+
the type of all values. The const qualifier for arrays specifies
417+
const-ness of the array size, so you are allowed to change existing array
418+
values, but you may not add or delete array entries.
419+
420+
my const @a = (0..4);
421+
$a[0] = 'ok';
422+
push @a, 'bad';
423+
=> Modification of a read-only value attempted
424+
425+
Hash keys are always of type string, so the hash type declaration specifies
426+
the type of all values. The const qualifier for hashes specifies
427+
const-ness of all keys, so you are allowed to change hash values, but
428+
you may not add or delete or change hash keys.
429+
430+
my const %h = ('ok' => 0);
431+
$a{ok} = 1;
432+
$a{bad} = 1;
433+
=> Attempt to access disallowed key
434+
435+
const hashes are implemented as perfect hashes, with element access in O(1)
436+
time. Existing hashes can be converted to such perfect hashes with
437+
L<perlfunc/study>.
438+
439+
my %h;
440+
$h{$_}=1 for <>;
441+
study %h; # %h is now a PH, a perfect hash
442+
366443
=head3 Version Strings
367444
X<version string> X<vstring> X<v-string>
368445

0 commit comments

Comments
 (0)