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} = 1;
89 }
90
91 sub unimport {
92 $^H{myint} = 0;
93 }
94
95 sub in_effect {
96 my $level = shift // 0;
97 my $hinthash = (caller($level))[10];
98 return $hinthash->{myint};
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 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} when each line of the user's script was called,
129 and therefore provide the correct semantics in the subroutine
130 implementing the overloaded addition.
131
133 The optree is shared between threads. This means there is a
134 possibility that the optree will outlive the particular thread (and
135 therefore the interpreter instance) that created it, so true Perl
136 scalars cannot be stored in the optree. Instead a compact form is
137 used, which can only store values that are integers (signed and
138 unsigned), strings or "undef" - references and floating point values
139 are stringified. If you need to store multiple values or complex
140 structures, you should serialise them, for example with "pack". The
141 deletion of a hash key from "%^H" is recorded, and as ever can be
142 distinguished from the existence of a key with value "undef" with
143 "exists".
144
145 Don't attempt to store references to data structures as integers which
146 are retrieved via "caller" and converted back, as this will not be
147 threadsafe. Accesses would be to the structure without locking (which
148 is not safe for Perl's scalars), and either the structure has to leak,
149 or it has to be freed when its creating thread terminates, which may be
150 before the optree referencing it is deleted, if other threads outlive
151 it.
152
153
154
155perl v5.10.1 2009-02-12 PERLPRAGMA(1)