Debian Codegraph @

upstream/opensuse-tumbleweed/man1/perlpragma.1

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\"
56 .\" Required to disable full justification in groff 1.23.0.
57 .if n .ds AD l
58 .\" ========================================================================
59 .\"
60 .IX Title "PERLPRAGMA 1"
61 .TH PERLPRAGMA 1 2026-03-09 "perl v5.42.1" "Perl Programmers Reference Guide"
62 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
63 .\" way too many mistakes in technical documents.
64 .if n .ad l
65 .nh
66 .SH NAME
67 perlpragma \- how to write a user pragma
68 .SH DESCRIPTION
69 .IX Header "DESCRIPTION"
70 A pragma is a module which influences some aspect of the compile time or run
71 time behaviour of Perl, such as \f(CW\*(C`strict\*(C'\fR or \f(CW\*(C`warnings\*(C'\fR. With Perl 5.10 you
72 are no longer limited to the built in pragmata; you can now create user
73 pragmata that modify the behaviour of user functions within a lexical scope.
74 .SH "A basic example"
75 .IX Header "A basic example"
76 For example, say you need to create a class implementing overloaded
77 mathematical operators, and would like to provide your own pragma that
78 functions much like \f(CW\*(C`use integer;\*(C'\fR You\*(Aqd like this code
79 .PP
80 .Vb 1
81 \& use MyMaths;
82 \&
83 \& my $l = MyMaths\->new(1.2);
84 \& my $r = MyMaths\->new(3.4);
85 \&
86 \& print "A: ", $l + $r, "\en";
87 \&
88 \& use myint;
89 \& print "B: ", $l + $r, "\en";
90 \&
91 \& {
92 \& no myint;
93 \& print "C: ", $l + $r, "\en";
94 \& }
95 \&
96 \& print "D: ", $l + $r, "\en";
97 \&
98 \& no myint;
99 \& print "E: ", $l + $r, "\en";
100 .Ve
101 .PP
102 to give the output
103 .PP
104 .Vb 5
105 \& A: 4.6
106 \& B: 4
107 \& C: 4.6
108 \& D: 4
109 \& E: 4.6
110 .Ve
111 .PP
112 \&\fIi.e.\fR, where \f(CW\*(C`use myint;\*(C'\fR is in effect, addition operations are forced
113 to integer, whereas by default they are not, with the default behaviour being
114 restored via \f(CW\*(C`no myint;\*(C'\fR
115 .PP
116 The minimal implementation of the package \f(CW\*(C`MyMaths\*(C'\fR would be something like
117 this:
118 .PP
119 .Vb 12
120 \& package MyMaths;
121 \& use v5.36;
122 \& use myint();
123 \& use overload \*(Aq+\*(Aq => sub {
124 \& my ($l, $r) = @_;
125 \& # Pass 1 to check up one call level from here
126 \& if (myint::in_effect(1)) {
127 \& int($$l) + int($$r);
128 \& } else {
129 \& $$l + $$r;
130 \& }
131 \& };
132 \&
133 \& sub new {
134 \& my ($class, $value) = @_;
135 \& bless \e$value, $class;
136 \& }
137 \&
138 \& 1;
139 .Ve
140 .PP
141 Note how we load the user pragma \f(CW\*(C`myint\*(C'\fR with an empty list \f(CW\*(C`()\*(C'\fR to
142 prevent its \f(CW\*(C`import\*(C'\fR being called.
143 .PP
144 The interaction with the Perl compilation happens inside package \f(CW\*(C`myint\*(C'\fR:
145 .PP
146 .Vb 1
147 \& package myint;
148 \&
149 \& use v5.36;
150 \&
151 \& sub import {
152 \& $^H{"myint/in_effect"} = 1;
153 \& }
154 \&
155 \& sub unimport {
156 \& $^H{"myint/in_effect"} = 0;
157 \& }
158 \&
159 \& sub in_effect {
160 \& my $level = shift // 0;
161 \& my $hinthash = (caller($level))[10];
162 \& return $hinthash\->{"myint/in_effect"};
163 \& }
164 \&
165 \& 1;
166 .Ve
167 .PP
168 As pragmata are implemented as modules, like any other module, \f(CW\*(C`use myint;\*(C'\fR
169 becomes
170 .PP
171 .Vb 4
172 \& BEGIN {
173 \& require myint;
174 \& myint\->import();
175 \& }
176 .Ve
177 .PP
178 and \f(CW\*(C`no myint;\*(C'\fR is
179 .PP
180 .Vb 4
181 \& BEGIN {
182 \& require myint;
183 \& myint\->unimport();
184 \& }
185 .Ve
186 .PP
187 Hence the \f(CW\*(C`import\*(C'\fR and \f(CW\*(C`unimport\*(C'\fR routines are called at \fBcompile time\fR
188 for the user\*(Aqs code.
189 .PP
190 User pragmata store their state by writing to the magical hash \f(CW\*(C`%^H\*(C'\fR,
191 hence these two routines manipulate it. The state information in \f(CW\*(C`%^H\*(C'\fR is
192 stored in the optree, and can be retrieved read\-only at runtime with \f(CWcaller()\fR,
193 at index 10 of the list of returned results. In the example pragma, retrieval
194 is encapsulated into the routine \f(CWin_effect()\fR, which takes as parameter
195 the number of call frames to go up to find the value of the pragma in the
196 user\*(Aqs script. This uses \f(CWcaller()\fR to determine the value of
197 \&\f(CW$^H{"myint/in_effect"}\fR when each line of the user\*(Aqs script was called, and
198 therefore provide the correct semantics in the subroutine implementing the
199 overloaded addition.
200 .SH "Key naming"
201 .IX Header "Key naming"
202 There is only a single \f(CW\*(C`%^H\*(C'\fR, but arbitrarily many modules that want
203 to use its scoping semantics. To avoid stepping on each other\*(Aqs toes,
204 they need to be sure to use different keys in the hash. It is therefore
205 conventional for a module to use only keys that begin with the module\*(Aqs
206 name (the name of its main package) and a "/" character. After this
207 module\-identifying prefix, the rest of the key is entirely up to the
208 module: it may include any characters whatsoever. For example, a module
209 \&\f(CW\*(C`Foo::Bar\*(C'\fR should use keys such as \f(CW\*(C`Foo::Bar/baz\*(C'\fR and \f(CW\*(C`Foo::Bar/$%/_!\*(C'\fR.
210 Modules following this convention all play nicely with each other.
211 .PP
212 The Perl core uses a handful of keys in \f(CW\*(C`%^H\*(C'\fR which do not follow this
213 convention, because they predate it. Keys that follow the convention
214 won\*(Aqt conflict with the core\*(Aqs historical keys.
215 .SH "Implementation details"
216 .IX Header "Implementation details"
217 The optree is shared between threads. This means there is a possibility that
218 the optree will outlive the particular thread (and therefore the interpreter
219 instance) that created it, so true Perl scalars cannot be stored in the
220 optree. Instead a compact form is used, which can only store values that are
221 integers (signed and unsigned), strings or \f(CW\*(C`undef\*(C'\fR \- references and
222 floating point values are stringified. If you need to store multiple values
223 or complex structures, you should serialise them, for example with \f(CW\*(C`pack\*(C'\fR.
224 The deletion of a hash key from \f(CW\*(C`%^H\*(C'\fR is recorded, and as ever can be
225 distinguished from the existence of a key with value \f(CW\*(C`undef\*(C'\fR with
226 \&\f(CW\*(C`exists\*(C'\fR.
227 .PP
228 \&\fBDon\*(Aqt\fR attempt to store references to data structures as integers which
229 are retrieved via \f(CW\*(C`caller\*(C'\fR and converted back, as this will not be threadsafe.
230 Accesses would be to the structure without locking (which is not safe for
231 Perl\*(Aqs scalars), and either the structure has to leak, or it has to be
232 freed when its creating thread terminates, which may be before the optree
233 referencing it is deleted, if other threads outlive it.