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

« back to all changes in this revision

Viewing changes to functions/eval.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:
54
54
      <h2>Links:</h2>
55
55
      <ul>
56
56
        <li><a href="http://search.cpan.org">CPAN</a></li>
 
57
        <li><a href="http://www.perl.org">Perl.org</a></li>
57
58
        <li><a href="http://www.perl.com">Perl.com</a></li>
58
 
        <li><a href="http://www.perl.org">Perl.org</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>
59
62
        <li><a href="http://www.pm.org">Perl Mongers</a></li>
60
63
        <li><a href="http://www.perlmonks.org">Perl Monks</a></li>
61
64
        <li><a href="http://planet.perl.org">Planet Perl</a></li>
65
68
      <ul>
66
69
        <li>Site maintained by<br><a href="http://perl.jonallen.info">Jon Allen</a>
67
70
            (<a href="http://perl.jonallen.info">JJ</a>)</li>
68
 
        <li class="spaced">Last updated on<br>23 April 2006</li>
 
71
        <li class="spaced">Last updated on<br>23 December 2007</li>
69
72
        <li class="spaced">See the <a href="http://perl.jonallen.info/projects/perldoc">project page</a> for
70
73
        more details</li>
71
74
      </ul>
76
79
    <div id="centerContent">
77
80
      <div id="contentHeader">
78
81
        <div id="contentHeaderLeft"><a href="#" onClick="showLeft()">Show navigation</a></div>
79
 
        <div id="contentHeaderCentre">-- Perl 5.8.8 documentation --</div>
 
82
        <div id="contentHeaderCentre">-- Perl 5.10.0 documentation --</div>
80
83
        <div id="contentHeaderRight"><a href="#" onClick="showRight()">Show toolbar</a></div>
81
84
      </div>
82
85
      <div id="breadCrumbs"><a href="../index.html">Home</a> &gt; <a href="../index-language.html">Language reference</a> &gt; <a href="../index-functions.html">Functions</a> &gt; eval</div>
83
86
      <script language="JavaScript">fromSearch();</script>
84
87
      <div id="contentBody"><div class="title_container"><div class="page_title">eval</div></div><div class="mod_az_list"><a href="../index-functions.html">Perl functions A-Z</a> | <a href="../index-functions-by-cat.html">Perl functions by category</a> | <a href="../perlfunc.html">The 'perlfunc' manpage</a></div><ul>
85
88
<li><a name="eval-EXPR"></a><b>eval EXPR</b>
86
 
<p>     </p>
 
89
<p>     
 
90
 </p>
87
91
</li>
88
92
</ul>
89
93
<ul>
111
115
evaluated inside the mini-program; a return statement may be also used, just
112
116
as with subroutines.  The expression providing the return value is evaluated
113
117
in void, scalar, or list context, depending on the context of the <code class="inline"><a class="l_k" href="../functions/eval.html">eval</a></code> 
114
 
itself.  See <a href="wantarray.html">wantarray</a> for more on how the evaluation context can be 
 
118
itself.  See <a href="#wantarray">"wantarray"</a> for more on how the evaluation context can be 
115
119
determined.</p>
116
120
<p>If there is a syntax error or runtime error, or a <code class="inline"><a class="l_k" href="../functions/die.html">die</a></code> statement is
117
121
executed, an undefined value is returned by <code class="inline"><a class="l_k" href="../functions/eval.html">eval</a></code>, and <code class="inline"><span class="i">$@</span></code>
121
125
string.  Beware that using <code class="inline"><a class="l_k" href="../functions/eval.html">eval</a></code> neither silences perl from printing
122
126
warnings to STDERR, nor does it stuff the text of warning messages into <code class="inline"><span class="i">$@</span></code>
123
127
.
124
 
To do either of those, you have to use the <code class="inline"><span class="i">$SIG</span>{__WARN__}</code>
 
128
To do either of those, you have to use the <code class="inline"><span class="i">$SIG</span>{<span class="w">__WARN__</span>}</code>
125
129
 facility, or
126
 
turn off warnings inside the BLOCK or EXPR using <code&nbsp;class="inline">no&nbsp;warnings&nbsp;'all'</code>.
127
 
See <a href="warn.html">warn</a>, <a href="../perlvar.html">perlvar</a>, <a href="../warnings.html">warnings</a> and <a href="../perllexwarn.html">perllexwarn</a>.</p>
 
130
turn off warnings inside the BLOCK or EXPR using <code&nbsp;class="inline"><a&nbsp;class="l_k"&nbsp;href="../functions/no.html">no</a>&nbsp;<span&nbsp;class="w">warnings</span>&nbsp;<span&nbsp;class="q">&#39;all&#39;</span></code>&nbsp;.
 
131
See <a href="#warn">"warn"</a>, <a href="../perlvar.html">perlvar</a>, <a href="../warnings.html">warnings</a> and <a href="../perllexwarn.html">perllexwarn</a>.</p>
128
132
<p>Note that, because <code class="inline"><a class="l_k" href="../functions/eval.html">eval</a></code> traps otherwise-fatal errors, it is useful for
129
133
determining whether a particular feature (such as <code class="inline"><a class="l_k" href="../functions/socket.html">socket</a></code> or <code class="inline"><a class="l_k" href="../functions/symlink.html">symlink</a></code>)
130
134
is implemented.  It is also Perl's exception trapping mechanism, where
143
147
<pre class="verbatim">    <span class="c"># a run-time error</span>
144
148
    <a class="l_k" href="../functions/eval.html">eval</a> <span class="q">&#39;$answer =&#39;</span><span class="sc">;</span>   <span class="c"># sets $@</span></pre>
145
149
<p>Using the <code class="inline"><a class="l_k" href="../functions/eval.html">eval{}</a></code> form as an exception trap in libraries does have some
146
 
issues.  Due to the current arguably broken state of <code class="inline">__DIE__</code>
 
150
issues.  Due to the current arguably broken state of <code class="inline"><span class="w">__DIE__</span></code>
147
151
 hooks, you
148
 
may wish not to trigger any <code class="inline">__DIE__</code>
 
152
may wish not to trigger any <code class="inline"><span class="w">__DIE__</span></code>
149
153
 hooks that user code may have installed.
150
 
You can use the <code class="inline"><a class="l_k" href="../functions/local.html">local</a> <span class="i">$SIG</span>{__DIE__}</code>
 
154
You can use the <code class="inline"><a class="l_k" href="../functions/local.html">local</a> <span class="i">$SIG</span>{<span class="w">__DIE__</span>}</code>
151
155
 construct for this purpose,
152
156
as shown in this example:</p>
153
157
<pre class="verbatim">    <span class="c"># a very private exception trap for divide-by-zero</span>
154
158
    <a class="l_k" href="../functions/eval.html">eval</a> <span class="s">{</span> <a class="l_k" href="../functions/local.html">local</a> <span class="i">$SIG</span>{<span class="q">&#39;__DIE__&#39;</span>}<span class="sc">;</span> <span class="i">$answer</span> = <span class="i">$a</span> / <span class="i">$b</span><span class="sc">;</span> <span class="s">}</span><span class="sc">;</span>
155
159
    <a class="l_k" href="../functions/warn.html">warn</a> <span class="i">$@</span> if <span class="i">$@</span><span class="sc">;</span></pre>
156
 
<p>This is especially significant, given that <code class="inline">__DIE__</code>
 
160
<p>This is especially significant, given that <code class="inline"><span class="w">__DIE__</span></code>
157
161
 hooks can call
158
162
<code class="inline"><a class="l_k" href="../functions/die.html">die</a></code> again, which has the effect of changing their error messages:</p>
159
163
<pre class="verbatim">    <span class="c"># __DIE__ hooks may modify error messages</span>
184
188
normally you <i>would</i> like to use double quotes, except that in this
185
189
particular situation, you can just use symbolic references instead, as
186
190
in case 6.</p>
187
 
<p><code class="inline"><a class="l_k" href="../functions/eval.html">eval</a> BLOCK</code>
 
191
<p><code class="inline"><a class="l_k" href="../functions/eval.html">eval</a> <span class="w">BLOCK</span></code>
188
192
 does <i>not</i> count as a loop, so the loop control statements
189
193
<code class="inline"><a class="l_k" href="../functions/next.html">next</a></code>, <code class="inline"><a class="l_k" href="../functions/last.html">last</a></code>, or <code class="inline"><a class="l_k" href="../functions/redo.html">redo</a></code> cannot be used to leave or restart the block.</p>
190
194
<p>Note that as a very special case, an <code class="inline"><a class="l_k" href="../functions/eval.html">eval</a> <span class="q">&#39;&#39;</span></code>
191
 
 executed within the <code class="inline"><a class="l_w" href="../DB.html">DB</a></code>
 
195
 executed within the <code class="inline"><span class="w">DB</span></code>
 
196
 
192
197
package doesn't see the usual surrounding lexical scope, but rather the
193
198
scope of the first non-DB piece of code that called it. You don't normally
194
199
need to worry about this unless you are writing a Perl debugger.</p>
211
216
          <!--<select name="r"><option value="1" selected>Go to top result<option value="0">Show results list</select>-->
212
217
        </form>
213
218
      </p>
 
219
      <script language="JavaScript" type="text/javascript" src="/perl-version.js"></script>
214
220
      <h2>Labels:</h2>
215
221
      <p>
216
222
        <a href="#" onClick="addLabel('eval','functions/eval.html')">Add this page</a>