1Const::Fast(3pm) User Contributed Perl Documentation Const::Fast(3pm)
2
3
4
6 Const::Fast - Facility for creating read-only scalars, arrays, and
7 hashes
8
10 version 0.014
11
13 use Const::Fast;
14
15 const my $foo => 'a scalar value';
16 const my @bar => qw/a list value/;
17 const my %buz => (a => 'hash', of => 'something');
18
20 const $var, $value
21 const @var, @value...
22 const %var, %value...
23 This the only function of this module and it is exported by default. It
24 takes a scalar, array or hash lvalue as first argument, and a list of
25 one or more values depending on the type of the first argument as the
26 value for the variable. It will set the variable to that value and
27 subsequently make it readonly. Arrays and hashes will be made deeply
28 readonly.
29
30 Exporting is done using Sub::Exporter::Progressive. You may need to
31 depend on Sub::Exporter explicitly if you need the latter's
32 flexibility.
33
35 This module was written because I stumbled on some serious issues of
36 Readonly that aren't easily fixable without breaking backwards
37 compatibility in subtle ways. In particular Readonly's use of ties is a
38 source of subtle bugs and bad performance. Instead, this module uses
39 the builtin readonly feature of perl, making access to the variables
40 just as fast as any normal variable without the weird side-effects of
41 ties. Readonly can do the same for scalars when Readonly::XS is
42 installed, but chooses not to do so in the most common case. This may
43 change in the future if someone takes up maintenance of Readonly, and
44 the two modules may be convergence if that happens.
45
47 Perl doesn't distinguish between restricted hashes and readonly hashes.
48 This means that:
49
50 use Const::Fast;
51 const my %a => (foo => 1, bar => 2);
52 say 1 unless $a{baz}
53
54 Will give the error "Attempt to access disallowed key 'baz' in a
55 restricted hash". You have to use "exists $a{baz}" instead. This is a
56 limitation of perl that can hopefully be solved in the future.
57
59 The interface for this module was inspired by Eric Roode's Readonly.
60 The implementation is inspired by doing everything the opposite way
61 Readonly does it.
62
64 Leon Timmermans <fawaka@gmail.com>
65
67 This software is copyright (c) 2010 by Leon Timmermans.
68
69 This is free software; you can redistribute it and/or modify it under
70 the same terms as the Perl 5 programming language system itself.
71
72
73
74perl v5.38.0 2023-07-20 Const::Fast(3pm)