[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
doubts about functions declared in parse.h ...
From: |
John W. Eaton |
Subject: |
doubts about functions declared in parse.h ... |
Date: |
Fri, 11 Mar 2005 15:07:39 -0500 |
On 11-Mar-2005, Alberto Francisco Martin Huertas <address@hidden> wrote:
| Hello. I'm a spanish university student and I'm performing my final career
| project with Octave. I'm developing a DLF (Dinamycally Loaded Function) for
| Octave, and I'm interested in to know if there is any function declared in
| parse.h header file that allows you to parse and execute some octave script
| file without modifying some other enviroment variables used before. For
| example, if the following script file (called example.m) had the following
| sentence:
|
| A=3;
|
| and my DLD function were something like this:
|
| DEFUN_DLD(example_function, arguments, ,
| "a example function") {
| the_function_im_looking_for("example.m");
| return eval_string("A",...);
|
|
| }
|
| I would like the following behaviour in a Octave session:
|
| octave:1> A=1
| A = 1
| octave:2> example_function()
| ans = 3
| octave:2> A
| A = 1
The following will do what you want, but why do you want this
behavior? What are you really trying to do?
#include <octave/oct.h>
#include <octave/parse.h>
DEFUN_DLD (exfun, args, , "exfun (file, sym)")
{
octave_value_list retval;
if (args.length () == 2)
{
std::string fname = args(0).string_value ();
std::string sym = args(1).string_value ();
if (! error_state)
{
source_file (fname);
if (! error_state)
{
symbol_record *sr = curr_sym_tab->lookup (sym);
if (sr)
retval(0) = sr->def ();
else
error ("exfun: symbol \"%s\" not found", sym.c_str ());
}
}
}
else
print_usage ("exfun");
return retval;
}
Example of using this:
$ cat script.m
a = 2;
octave:1> exfun ("script.m", "a")
ans = 2
octave:2> a
a = 2
Note that the seemingly equivalent function
function retval = exfun (file, sym)
if (nargin == 2)
source (file);
retval = eval (sym);
else
usage ("exfun (file, sym)");
endif
endfunction
does not work, because functions like this introduce a local scope and
C++ functions do not. This is normally not a source of confusion,
because it is not common to source a file inside a C++ function.
jwe
-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.
Octave's home on the web: http://www.octave.org
How to fund new projects: http://www.octave.org/funding.html
Subscription information: http://www.octave.org/archive.html
-------------------------------------------------------------