File: Evaluation-in-a-Different-Context.html

package info (click to toggle)
octave3.2 3.2.4-8
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 62,936 kB
  • ctags: 37,353
  • sloc: cpp: 219,497; fortran: 116,336; ansic: 10,264; sh: 5,508; makefile: 4,245; lex: 3,573; yacc: 3,062; objc: 2,042; lisp: 1,692; awk: 860; perl: 844
file content (136 lines) | stat: -rw-r--r-- 6,281 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<html lang="en">
<head>
<title>Evaluation in a Different Context - Untitled</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Untitled">
<meta name="generator" content="makeinfo 4.11">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Evaluation.html#Evaluation" title="Evaluation">
<link rel="prev" href="Calling-a-Function-by-its-Name.html#Calling-a-Function-by-its-Name" title="Calling a Function by its Name">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
  pre.display { font-family:inherit }
  pre.format  { font-family:inherit }
  pre.smalldisplay { font-family:inherit; font-size:smaller }
  pre.smallformat  { font-family:inherit; font-size:smaller }
  pre.smallexample { font-size:smaller }
  pre.smalllisp    { font-size:smaller }
  span.sc    { font-variant:small-caps }
  span.roman { font-family:serif; font-weight:normal; } 
  span.sansserif { font-family:sans-serif; font-weight:normal; } 
--></style>
</head>
<body>
<div class="node">
<p>
<a name="Evaluation-in-a-Different-Context"></a>
Previous:&nbsp;<a rel="previous" accesskey="p" href="Calling-a-Function-by-its-Name.html#Calling-a-Function-by-its-Name">Calling a Function by its Name</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Evaluation.html#Evaluation">Evaluation</a>
<hr>
</div>

<h3 class="section">9.2 Evaluation in a Different Context</h3>

<p>Before you evaluate an expression you need to substitute
the values of the variables used in the expression.  These
are stored in the symbol table.  Whenever the interpreter
starts a new function it saves the current symbol table
and creates a new one, initializing it with the list of
function parameters and a couple of predefined variables
such as <code>nargin</code>.  Expressions inside the function use the
new symbol table.

   <p>Sometimes you want to write a function so that when you
call it, it modifies variables in your own context.  This
allows you to use a pass-by-name style of function,
which is similar to using a pointer in programming languages such
as C.

   <p>Consider how you might write <code>save</code> and <code>load</code> as
m-files.  For example,

<pre class="example">     function create_data
       x = linspace (0, 10, 10);
       y = sin (x);
       save mydata x y
     endfunction
</pre>
   <p>With <code>evalin</code>, you could write <code>save</code> as follows:

<pre class="example">     function save (file, name1, name2)
       f = open_save_file (file);
       save_var(f, name1, evalin ("caller", name1));
       save_var(f, name2, evalin ("caller", name2));
     endfunction
</pre>
   <p class="noindent">Here, &lsquo;<samp><span class="samp">caller</span></samp>&rsquo; is the <code>create_data</code> function and <code>name1</code>
is the string <code>"x"</code>, which evaluates simply as the value of <code>x</code>.

   <p>You later want to load the values back from <code>mydata</code>
in a different context:

<pre class="example">     function process_data
       load mydata
       ... do work ...
     endfunction
</pre>
   <p class="noindent">With <code>assignin</code>, you could write <code>load</code> as follows:

<pre class="example">     function load (file)
       f = open_load_file (file);
       [name, val] = load_var (f);
       assignin ("caller", name, val);
       [name, val] = load_var (f);
       assignin ("caller", name, val);
     endfunction
</pre>
   <p class="noindent">Here, &lsquo;<samp><span class="samp">caller</span></samp>&rsquo; is the <code>process_data</code> function.

   <p>You can set and use variables at the command prompt
using the context &lsquo;<samp><span class="samp">base</span></samp>&rsquo; rather than &lsquo;<samp><span class="samp">caller</span></samp>&rsquo;.

   <p>These functions are rarely used in practice.  One
example is the <code>fail (&lsquo;</code><samp><span class="samp">code</span></samp><code>&rsquo;, &lsquo;</code><samp><span class="samp">pattern</span></samp><code>&rsquo;)</code> function
which evaluates &lsquo;<samp><span class="samp">code</span></samp>&rsquo; in the caller's context and
checks that the error message it produces matches
the given pattern.  Other examples such as <code>save</code> and <code>load</code>
are written in C++ where all Octave variables
are in the &lsquo;<samp><span class="samp">caller</span></samp>&rsquo; context and <code>evalin</code> is not needed.

<!-- parse.cc -->
   <p><a name="doc_002devalin"></a>

<div class="defun">
&mdash; Built-in Function:  <b>evalin</b> (<var>context, try, catch</var>)<var><a name="index-evalin-552"></a></var><br>
<blockquote><p>Like <code>eval</code>, except that the expressions are evaluated in the
context <var>context</var>, which may be either <code>"caller"</code> or
<code>"base"</code>. 
</p></blockquote></div>

<!-- parse.cc -->
   <p><a name="doc_002dassignin"></a>

<div class="defun">
&mdash; Built-in Function:  <b>assignin</b> (<var>context, varname, value</var>)<var><a name="index-assignin-553"></a></var><br>
<blockquote><p>Assign <var>value</var> to <var>varname</var> in context <var>context</var>, which
may be either <code>"base"</code> or <code>"caller"</code>. 
</p></blockquote></div>

<!-- DO NOT EDIT!  Generated automatically by munge-texi. -->
<!-- Copyright (C) 1996, 1997, 1999, 2000, 2002, 2003, 2004, 2007, 2008, 2009 -->
<!-- John W. Eaton -->
<!-- This file is part of Octave. -->
<!-- Octave is free software; you can redistribute it and/or modify it -->
<!-- under the terms of the GNU General Public License as published by the -->
<!-- Free Software Foundation; either version 3 of the License, or (at -->
<!-- your option) any later version. -->
<!-- Octave is distributed in the hope that it will be useful, but WITHOUT -->
<!-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -->
<!-- FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License -->
<!-- for more details. -->
<!-- You should have received a copy of the GNU General Public License -->
<!-- along with Octave; see the file COPYING.  If not, see -->
<!-- <http://www.gnu.org/licenses/>. -->
   </body></html>