~ubuntu-branches/ubuntu/intrepid/perl-doc-html/intrepid

« back to all changes in this revision

Viewing changes to perlpragma.html

  • Committer: Bazaar Package Importer
  • Author(s): Roberto C. Sanchez
  • Date: 2008-05-17 20:14:19 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080517201419-qgbuogq2ckkdisyi
Tags: 5.10.0-2
Supersede botched upload of 5.10.0-1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
2
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
 
3
<head>
 
4
  <title>perlpragma - perldoc.perl.org</title>
 
5
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
 
6
  <meta http-equiv="Content-Language" content="en-gb" />
 
7
  <link href="css.css" rel="stylesheet" rev="stylesheet" type="text/css" media="screen" />
 
8
</head>
 
9
 
 
10
<script language="JavaScript" type="text/javascript" src="label.js"></script>
 
11
 
 
12
<script language="JavaScript">
 
13
  pageDepth = 0;
 
14
  setPath();
 
15
</script>
 
16
 
 
17
<body onLoad="showToolbars();loadLabels()">
 
18
 
 
19
<div id="pageHeader">
 
20
  <div id="pageHeaderLogo">
 
21
    <img src="onion.gif">
 
22
  </div>
 
23
  <div id="pageHeaderText">
 
24
    <a href="http://perldoc.perl.org">perldoc.perl.org</a>
 
25
  </div>
 
26
</div>
 
27
 
 
28
<div id="pageBody">
 
29
  <div id="left">
 
30
    <div id="leftContent">
 
31
      <div id="leftClose">
 
32
        <a href="#" onClick="closeLeft()" title="Hide navigation" onmouseover="leftCloseIcon.src='close_purple.gif';" onmouseout="leftCloseIcon.src='close_blue.gif';"><img src="close_blue.gif" name="leftCloseIcon" id="leftCloseIcon" border=0></a>
 
33
      </div>
 
34
      <h1>Manual:</h1>
 
35
      <ul>
 
36
        <li><a href="index-overview.html">Overview</a></li>
 
37
        <li><a href="index-tutorials.html">Tutorials</a></li>
 
38
        <li><a href="index-faq.html">FAQs</a></li>
 
39
        <li><a href="index-history.html">History / Changes</a></li>
 
40
        <li><a href="index-licence.html">Licence</a></li>
 
41
      </ul>
 
42
      <h2>Reference:</h2>
 
43
      <ul>
 
44
        <li><a href="index-language.html">Language</a></li>
 
45
        <li><a href="index-functions.html">Functions</a></li>
 
46
        <li><a href="perlop.html">Operators</a></li>
 
47
        <li><a href="perlvar.html">Special variables</a></li>
 
48
        <li><a href="index-pragmas.html">Pragmas</a></li>
 
49
        <li><a href="index-modules-A.html">Core modules</a></li>
 
50
        <li><a href="index-utilities.html">Utilities</a></li>
 
51
        <li><a href="index-internals.html">Internals</a></li>
 
52
        <li><a href="index-platforms.html">Platform specific</a></li>
 
53
      </ul>
 
54
      <h2>Links:</h2>
 
55
      <ul>
 
56
        <li><a href="http://search.cpan.org">CPAN</a></li>
 
57
        <li><a href="http://www.perl.org">Perl.org</a></li>
 
58
        <li><a href="http://www.perl.com">Perl.com</a></li>
 
59
        <li><a href="http://perlbuzz.com">Perl Buzz</a></li>
 
60
        <li><a href="http://www.perlfoundation.org/perl5/index.cgi">Perl 5 Wiki</a></li>
 
61
        <li><a href="http://jobs.perl.org">Perl Jobs</a></li>
 
62
        <li><a href="http://www.pm.org">Perl Mongers</a></li>
 
63
        <li><a href="http://www.perlmonks.org">Perl Monks</a></li>
 
64
        <li><a href="http://planet.perl.org">Planet Perl</a></li>
 
65
        <li><a href="http://use.perl.org">Use Perl</a></li>
 
66
      </ul>
 
67
      <h2>Contact:</h2>
 
68
      <ul>
 
69
        <li>Site maintained by<br><a href="http://perl.jonallen.info">Jon Allen</a>
 
70
            (<a href="http://perl.jonallen.info">JJ</a>)</li>
 
71
        <li class="spaced">Last updated on<br>23 December 2007</li>
 
72
        <li class="spaced">See the <a href="http://perl.jonallen.info/projects/perldoc">project page</a> for
 
73
        more details</li>
 
74
      </ul>
 
75
    </div>
 
76
  </div>
 
77
 
 
78
  <div id="center">  
 
79
    <div id="centerContent">
 
80
      <div id="contentHeader">
 
81
        <div id="contentHeaderLeft"><a href="#" onClick="showLeft()">Show navigation</a></div>
 
82
        <div id="contentHeaderCentre">-- Perl 5.10.0 documentation --</div>
 
83
        <div id="contentHeaderRight"><a href="#" onClick="showRight()">Show toolbar</a></div>
 
84
      </div>
 
85
      <div id="breadCrumbs"><a href="index.html">Home</a> &gt; <a href="index-tutorials.html">Tutorials</a> &gt; perlpragma</div>
 
86
      <script language="JavaScript">fromSearch();</script>
 
87
      <div id="contentBody"><div class="title_container"><div class="page_title">perlpragma</div></div><ul><li><a href="#NAME">NAME</a><li><a href="#DESCRIPTION">DESCRIPTION</a><li><a href="#A-basic-example">A basic example</a><li><a href="#Implementation-details">Implementation details</a></ul><a name="NAME"></a><h1>NAME</h1>
 
88
<p>perlpragma - how to write a user pragma</p>
 
89
<a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
 
90
<p>A pragma is a module which influences some aspect of the compile time or run
 
91
time behaviour of Perl, such as <code class="inline"><span class="w">strict</span></code>
 
92
 or <code class="inline"><span class="w">warnings</span></code>
 
93
. With Perl 5.10 you
 
94
are no longer limited to the built in pragmata; you can now create user
 
95
pragmata that modify the behaviour of user functions within a lexical scope.</p>
 
96
<a name="A-basic-example"></a><h1>A basic example</h1>
 
97
<p>For example, say you need to create a class implementing overloaded
 
98
mathematical operators, and would like to provide your own pragma that
 
99
functions much like <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">integer</span><span class="sc">;</span></code>
 
100
 You'd like this code</p>
 
101
<pre class="verbatim">    <a class="l_k" href="functions/use.html">use</a> <span class="w">MyMaths</span><span class="sc">;</span></pre>
 
102
<pre class="verbatim">    <a class="l_k" href="functions/my.html">my</a> <span class="i">$l</span> = <span class="w">MyMaths</span><span class="w">-&gt;new</span><span class="s">(</span><span class="n">1.2</span><span class="s">)</span><span class="sc">;</span>
 
103
    <a class="l_k" href="functions/my.html">my</a> <span class="i">$r</span> = <span class="w">MyMaths</span><span class="w">-&gt;new</span><span class="s">(</span><span class="n">3.4</span><span class="s">)</span><span class="sc">;</span></pre>
 
104
<pre class="verbatim">    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;A: &quot;</span><span class="cm">,</span> <span class="i">$l</span> + <span class="i">$r</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></pre>
 
105
<pre class="verbatim">    <a class="l_k" href="functions/use.html">use</a> <span class="w">myint</span><span class="sc">;</span>
 
106
    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;B: &quot;</span><span class="cm">,</span> <span class="i">$l</span> + <span class="i">$r</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></pre>
 
107
<pre class="verbatim">    <span class="s">{</span>
 
108
        <a class="l_k" href="functions/no.html">no</a> <span class="w">myint</span><span class="sc">;</span>
 
109
        <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;C: &quot;</span><span class="cm">,</span> <span class="i">$l</span> + <span class="i">$r</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span>
 
110
    <span class="s">}</span></pre>
 
111
<pre class="verbatim">    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;D: &quot;</span><span class="cm">,</span> <span class="i">$l</span> + <span class="i">$r</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></pre>
 
112
<pre class="verbatim">    <a class="l_k" href="functions/no.html">no</a> <span class="w">myint</span><span class="sc">;</span>
 
113
    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;E: &quot;</span><span class="cm">,</span> <span class="i">$l</span> + <span class="i">$r</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></pre>
 
114
<p>to give the output</p>
 
115
<pre class="verbatim">    A: 4.6
 
116
    B: 4
 
117
    C: 4.6
 
118
    D: 4
 
119
    E: 4.6</pre><p><i>i.e.</i>, where <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">myint</span><span class="sc">;</span></code>
 
120
 is in effect, addition operations are forced
 
121
to integer, whereas by default they are not, with the default behaviour being
 
122
restored via <code class="inline"><a class="l_k" href="functions/no.html">no</a> <span class="w">myint</span><span class="sc">;</span></code>
 
123
</p>
 
124
<p>The minimal implementation of the package <code class="inline"><span class="w">MyMaths</span></code>
 
125
 would be something like
 
126
this:</p>
 
127
<pre class="verbatim"><a name="package-MyMaths"></a>    package <span class="i">MyMaths</span><span class="sc">;</span>
 
128
    <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span><span class="sc">;</span>
 
129
    <a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span><span class="sc">;</span>
 
130
    <a class="l_k" href="functions/use.html">use</a> <span class="i">myint</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span>
 
131
    <a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span> <span class="q">'+'</span> <span class="cm">=&gt;</span> <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span>
 
132
        <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$l</span><span class="cm">,</span> <span class="i">$r</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span>
 
133
        <span class="c"># Pass 1 to check up one call level from here</span>
 
134
        if <span class="s">(</span><span class="i">myint::in_effect</span><span class="s">(</span><span class="n">1</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span>
 
135
            <a class="l_k" href="functions/int.html">int</a><span class="s">(</span><span class="i">$$l</span><span class="s">)</span> + <a class="l_k" href="functions/int.html">int</a><span class="s">(</span><span class="i">$$r</span><span class="s">)</span><span class="sc">;</span>
 
136
        <span class="s">}</span> else <span class="s">{</span>
 
137
            <span class="i">$$l</span> + <span class="i">$$r</span><span class="sc">;</span>
 
138
        <span class="s">}</span>
 
139
    <span class="s">}</span><span class="sc">;</span></pre>
 
140
<pre class="verbatim"><a name="new"></a>    sub <span class="m">new</span> <span class="s">{</span>
 
141
        <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$class</span><span class="cm">,</span> <span class="i">$value</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span>
 
142
        <a class="l_k" href="functions/bless.html">bless</a> \<span class="i">$value</span><span class="cm">,</span> <span class="i">$class</span><span class="sc">;</span>
 
143
    <span class="s">}</span></pre>
 
144
<pre class="verbatim">    <span class="n">1</span><span class="sc">;</span></pre>
 
145
<p>Note how we load the user pragma <code class="inline"><span class="w">myint</span></code>
 
146
 with an empty list <code class="inline"><span class="s">(</span><span class="s">)</span></code>
 
147
 to
 
148
prevent its <code class="inline"><a class="l_k" href="functions/import.html">import</a></code> being called.</p>
 
149
<p>The interaction with the Perl compilation happens inside package <code class="inline"><span class="w">myint</span></code>
 
150
:</p>
 
151
<pre class="verbatim"><a name="package-myint"></a>    package <span class="i">myint</span><span class="sc">;</span></pre>
 
152
<pre class="verbatim">    <a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span><span class="sc">;</span>
 
153
    <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span><span class="sc">;</span></pre>
 
154
<pre class="verbatim"><a name="import"></a>    sub <span class="m">import</span> <span class="s">{</span>
 
155
        <span class="i">$^H</span>{<span class="w">myint</span>} = <span class="n">1</span><span class="sc">;</span>
 
156
    <span class="s">}</span></pre>
 
157
<pre class="verbatim"><a name="unimport"></a>    sub <span class="m">unimport</span> <span class="s">{</span>
 
158
        <span class="i">$^H</span>{<span class="w">myint</span>} = <span class="n">0</span><span class="sc">;</span>
 
159
    <span class="s">}</span></pre>
 
160
<pre class="verbatim"><a name="in_effect"></a>    sub <span class="m">in_effect</span> <span class="s">{</span>
 
161
        <a class="l_k" href="functions/my.html">my</a> <span class="i">$level</span> = <a class="l_k" href="functions/shift.html">shift</a> // <span class="n">0</span><span class="sc">;</span>
 
162
        <a class="l_k" href="functions/my.html">my</a> <span class="i">$hinthash</span> = <span class="s">(</span><a class="l_k" href="functions/caller.html">caller</a><span class="s">(</span><span class="i">$level</span><span class="s">)</span><span class="s">)</span>[<span class="n">10</span>]<span class="sc">;</span>
 
163
        <a class="l_k" href="functions/return.html">return</a> <span class="i">$hinthash</span>-&gt;{<span class="w">myint</span>}<span class="sc">;</span>
 
164
    <span class="s">}</span></pre>
 
165
<pre class="verbatim">    <span class="n">1</span><span class="sc">;</span></pre>
 
166
<p>As pragmata are implemented as modules, like any other module, <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">myint</span><span class="sc">;</span></code>
 
167
 
 
168
becomes</p>
 
169
<pre class="verbatim">    BEGIN <span class="s">{</span>
 
170
        <a class="l_k" href="functions/require.html">require</a> <span class="w">myint</span><span class="sc">;</span>
 
171
        <span class="w">myint</span><span class="w">-&gt;import</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span>
 
172
    <span class="s">}</span></pre>
 
173
<p>and <code class="inline"><a class="l_k" href="functions/no.html">no</a> <span class="w">myint</span><span class="sc">;</span></code>
 
174
 is</p>
 
175
<pre class="verbatim">    BEGIN <span class="s">{</span>
 
176
        <a class="l_k" href="functions/require.html">require</a> <span class="w">myint</span><span class="sc">;</span>
 
177
        <span class="w">myint</span><span class="w">-&gt;unimport</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span>
 
178
    <span class="s">}</span></pre>
 
179
<p>Hence the <code class="inline"><a class="l_k" href="functions/import.html">import</a></code> and <code class="inline"><span class="w">unimport</span></code>
 
180
 routines are called at <b>compile time</b>
 
181
for the user's code.</p>
 
182
<p>User pragmata store their state by writing to the magical hash <code class="inline"><span class="i">%^H</span></code>
 
183
,
 
184
hence these two routines manipulate it. The state information in <code class="inline"><span class="i">%^H</span></code>
 
185
 is
 
186
stored in the optree, and can be retrieved at runtime with <code class="inline"><a class="l_k" href="functions/caller.html">caller()</a></code>, at
 
187
index 10 of the list of returned results. In the example pragma, retrieval
 
188
is encapsulated into the routine <code class="inline"><span class="i">in_effect</span><span class="s">(</span><span class="s">)</span></code>
 
189
, which takes as parameter
 
190
the number of call frames to go up to find the value of the pragma in the
 
191
user's script. This uses <code class="inline"><a class="l_k" href="functions/caller.html">caller()</a></code> to determine the value of
 
192
<code class="inline"><span class="i">$^H</span>{<span class="w">myint</span>}</code>
 
193
 when each line of the user's script was called, and
 
194
therefore provide the correct semantics in the subroutine implementing the
 
195
overloaded addition.</p>
 
196
<a name="Implementation-details"></a><h1>Implementation details</h1>
 
197
<p>The optree is shared between threads.  This means there is a possibility that
 
198
the optree will outlive the particular thread (and therefore the interpreter
 
199
instance) that created it, so true Perl scalars cannot be stored in the
 
200
optree.  Instead a compact form is used, which can only store values that are
 
201
integers (signed and unsigned), strings or <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> - references and
 
202
floating point values are stringified.  If you need to store multiple values
 
203
or complex structures, you should serialise them, for example with <code class="inline"><a class="l_k" href="functions/pack.html">pack</a></code>.
 
204
The deletion of a hash key from <code class="inline"><span class="i">%^H</span></code>
 
205
 is recorded, and as ever can be
 
206
distinguished from the existence of a key with value <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> with
 
207
<code class="inline"><a class="l_k" href="functions/exists.html">exists</a></code>.</p>
 
208
<p><b>Don't</b> attempt to store references to data structures as integers which
 
209
are retrieved via <code class="inline"><a class="l_k" href="functions/caller.html">caller</a></code> and converted back, as this will not be threadsafe.
 
210
Accesses would be to the structure without locking (which is not safe for
 
211
Perl's scalars), and either the structure has to leak, or it has to be
 
212
freed when its creating thread terminates, which may be before the optree
 
213
referencing it is deleted, if other threads outlive it.</p>
 
214
</div>
 
215
      <div id="contentFooter"><a href="http://www.perl.org"><img src="perlpowered.png" border=0></a></div>
 
216
    </div>
 
217
  </div>
 
218
 
 
219
  <div id="right">
 
220
    <div id="rightContent">
 
221
      <div id="leftClose">
 
222
        <a href="#" onClick="closeRight()" title="Hide toolbar" onmouseover="rightCloseIcon.src='close_purple.gif';" onmouseout="rightCloseIcon.src='close_blue.gif';"><img src="close_blue.gif" name="rightCloseIcon" id="rightCloseIcon" border=0></a>
 
223
      </div>
 
224
      <h1>Search:</h1>
 
225
      <p>
 
226
        <form action="search.html" name="perldoc_search">
 
227
          <input type="text" name="q" size="10" class="grey"><br>
 
228
          <!--<select name="r"><option value="1" selected>Go to top result<option value="0">Show results list</select>-->
 
229
        </form>
 
230
      </p>
 
231
      <script language="JavaScript" type="text/javascript" src="/perl-version.js"></script>
 
232
      <h2>Labels:</h2>
 
233
      <p>
 
234
        <a href="#" onClick="addLabel('perlpragma','perlpragma.html')">Add this page</a>
 
235
      </p>
 
236
      <div class="labels" id="labels">
 
237
      </div>
 
238
    </div>
 
239
  </div>
 
240
 
 
241
</div>
 
242
 
 
243
</body>
 
244
</html>