1PERLPRAGMA(1) Perl Programmers Reference Guide PERLPRAGMA(1)
2
3
4
6 perlpragma - how to write a user pragma
7
9 A pragma is a module which influences some aspect of the compile time
10 or run time behaviour of Perl, such as "strict" or "warnings". With
11 Perl 5.10 you are no longer limited to the built in pragmata; you can
12 now create user pragmata that modify the behaviour of user functions
13 within a lexical scope.
14
16 For example, say you need to create a class implementing overloaded
17 mathematical operators, and would like to provide your own pragma that
18 functions much like "use integer;" You'd like this code
19
20 use MyMaths;
21
22 my $l = MyMaths->new(1.2);
23 my $r = MyMaths->new(3.4);
24
25 print "A: ", $l + $r, "\n";
26
27 use myint;
28 print "B: ", $l + $r, "\n";
29
30 {
31 no myint;
32 print "C: ", $l + $r, "\n";
33 }
34
35 print "D: ", $l + $r, "\n";
36
37 no myint;
38 print "E: ", $l + $r, "\n";
39
40 to give the output
41
42 A: 4.6
43 B: 4
44 C: 4.6
45 D: 4
46 E: 4.6
47
48 i.e., where "use myint;" is in effect, addition operations are forced
49 to integer, whereas by default they are not, with the default behaviour
50 being restored via "no myint;"
51
52 The minimal implementation of the package "MyMaths" would be something
53 like this:
54
55 package MyMaths;
56 use warnings;
57 use strict;
58 use myint();
59 use overload '+' => sub {
60 my ($l, $r) = @_;
61 # Pass 1 to check up one call level from here
62 if (myint::in_effect(1)) {
63 int($$l) + int($$r);
64 } else {
65 $$l + $$r;
66 }
67 };
68
69 sub new {
70 my ($class, $value) = @_;
71 bless \$value, $class;
72 }
73
74 1;
75
76 Note how we load the user pragma "myint" with an empty list "()" to
77 prevent its "import" being called.
78
79 The interaction with the Perl compilation happens inside package
80 "myint":
81
82 package myint;
83
84 use strict;
85 use warnings;
86
87 sub import {
88 $^H{"myint/in_effect"} = 1;
89 }
90
91 sub unimport {
92 $^H{"myint/in_effect"} = 0;
93 }
94
95 sub in_effect {
96 my $level = shift // 0;
97 my $hinthash = (caller($level))[10];
98 return $hinthash->{"myint/in_effect"};
99 }
100
101 1;
102
103 As pragmata are implemented as modules, like any other module, "use
104 myint;" becomes
105
106 BEGIN {
107 require myint;
108 myint->import();
109 }
110
111 and "no myint;" is
112
113 BEGIN {
114 require myint;
115 myint->unimport();
116 }
117
118 Hence the "import" and "unimport" routines are called at compile time
119 for the user's code.
120
121 User pragmata store their state by writing to the magical hash "%^H",
122 hence these two routines manipulate it. The state information in "%^H"
123 is stored in the optree, and can be retrieved read-only at runtime with
124 "caller()", at index 10 of the list of returned results. In the example
125 pragma, retrieval is encapsulated into the routine "in_effect()", which
126 takes as parameter the number of call frames to go up to find the value
127 of the pragma in the user's script. This uses "caller()" to determine
128 the value of $^H{"myint/in_effect"} when each line of the user's script
129 was called, and therefore provide the correct semantics in the
130 subroutine implementing the overloaded addition.
131
133 There is only a single "%^H", but arbitrarily many modules that want to
134 use its scoping semantics. To avoid stepping on each other's toes,
135 they need to be sure to use different keys in the hash. It is
136 therefore conventional for a module to use only keys that begin with
137 the module's name (the name of its main package) and a "/" character.
138 After this module-identifying prefix, the rest of the key is entirely
139 up to the module: it may include any characters whatsoever. For
140 example, a module "Foo::Bar" should use keys such as "Foo::Bar/baz" and
141 "Foo::Bar/$%/_!". Modules following this convention all play nicely
142 with each other.
143
144 The Perl core uses a handful of keys in "%^H" which do not follow this
145 convention, because they predate it. Keys that follow the convention
146 won't conflict with the core's historical keys.
147
149 The optree is shared between threads. This means there is a
150 possibility that the optree will outlive the particular thread (and
151 therefore the interpreter instance) that created it, so true Perl
152 scalars cannot be stored in the optree. Instead a compact form is
153 used, which can only store values that are integers (signed and
154 unsigned), strings or "undef" - references and floating point values
155 are stringified. If you need to store multiple values or complex
156 structures, you should serialise them, for example with "pack". The
157 deletion of a hash key from "%^H" is recorded, and as ever can be
158 distinguished from the existence of a key with value "undef" with
159 "exists".
160
161 Don't attempt to store references to data structures as integers which
162 are retrieved via "caller" and converted back, as this will not be
163 threadsafe. Accesses would be to the structure without locking (which
164 is not safe for Perl's scalars), and either the structure has to leak,
165 or it has to be freed when its creating thread terminates, which may be
166 before the optree referencing it is deleted, if other threads outlive
167 it.
168
169
170
171perl v5.34.0 2021-10-18 PERLPRAGMA(1)