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 v5.36;
57 use myint();
58 use overload '+' => sub {
59 my ($l, $r) = @_;
60 # Pass 1 to check up one call level from here
61 if (myint::in_effect(1)) {
62 int($$l) + int($$r);
63 } else {
64 $$l + $$r;
65 }
66 };
67
68 sub new {
69 my ($class, $value) = @_;
70 bless \$value, $class;
71 }
72
73 1;
74
75 Note how we load the user pragma "myint" with an empty list "()" to
76 prevent its "import" being called.
77
78 The interaction with the Perl compilation happens inside package
79 "myint":
80
81 package myint;
82
83 use v5.36;
84
85 sub import {
86 $^H{"myint/in_effect"} = 1;
87 }
88
89 sub unimport {
90 $^H{"myint/in_effect"} = 0;
91 }
92
93 sub in_effect {
94 my $level = shift // 0;
95 my $hinthash = (caller($level))[10];
96 return $hinthash->{"myint/in_effect"};
97 }
98
99 1;
100
101 As pragmata are implemented as modules, like any other module, "use
102 myint;" becomes
103
104 BEGIN {
105 require myint;
106 myint->import();
107 }
108
109 and "no myint;" is
110
111 BEGIN {
112 require myint;
113 myint->unimport();
114 }
115
116 Hence the "import" and "unimport" routines are called at compile time
117 for the user's code.
118
119 User pragmata store their state by writing to the magical hash "%^H",
120 hence these two routines manipulate it. The state information in "%^H"
121 is stored in the optree, and can be retrieved read-only at runtime with
122 caller(), at index 10 of the list of returned results. In the example
123 pragma, retrieval is encapsulated into the routine in_effect(), which
124 takes as parameter the number of call frames to go up to find the value
125 of the pragma in the user's script. This uses caller() to determine the
126 value of $^H{"myint/in_effect"} when each line of the user's script was
127 called, and therefore provide the correct semantics in the subroutine
128 implementing the overloaded addition.
129
131 There is only a single "%^H", but arbitrarily many modules that want to
132 use its scoping semantics. To avoid stepping on each other's toes,
133 they need to be sure to use different keys in the hash. It is
134 therefore conventional for a module to use only keys that begin with
135 the module's name (the name of its main package) and a "/" character.
136 After this module-identifying prefix, the rest of the key is entirely
137 up to the module: it may include any characters whatsoever. For
138 example, a module "Foo::Bar" should use keys such as "Foo::Bar/baz" and
139 "Foo::Bar/$%/_!". Modules following this convention all play nicely
140 with each other.
141
142 The Perl core uses a handful of keys in "%^H" which do not follow this
143 convention, because they predate it. Keys that follow the convention
144 won't conflict with the core's historical keys.
145
147 The optree is shared between threads. This means there is a
148 possibility that the optree will outlive the particular thread (and
149 therefore the interpreter instance) that created it, so true Perl
150 scalars cannot be stored in the optree. Instead a compact form is
151 used, which can only store values that are integers (signed and
152 unsigned), strings or "undef" - references and floating point values
153 are stringified. If you need to store multiple values or complex
154 structures, you should serialise them, for example with "pack". The
155 deletion of a hash key from "%^H" is recorded, and as ever can be
156 distinguished from the existence of a key with value "undef" with
157 "exists".
158
159 Don't attempt to store references to data structures as integers which
160 are retrieved via "caller" and converted back, as this will not be
161 threadsafe. Accesses would be to the structure without locking (which
162 is not safe for Perl's scalars), and either the structure has to leak,
163 or it has to be freed when its creating thread terminates, which may be
164 before the optree referencing it is deleted, if other threads outlive
165 it.
166
167
168
169perl v5.38.2 2023-11-30 PERLPRAGMA(1)