|
From: Jim A. <ji...@tr...> - 2001-07-12 02:38:58
|
Mark Ackerman wrote:
>
> I've got an embedded Jython interpretor as my glue in a java program.
> I'm trying to do an exec or eval (both actually), sending an entire program
> in as a string. It's not working. I can use the exact same program as
> a file using execfile, and then it's fine.
>
> Any suggestions? I'm using \n as my line delimiter, if that helps.
It doesn't work like that: 'exec' only takes one 'unit' of code;
although you can define a function like this:
interp.exec(
"def prt(x): \n" +
" print x \n" +
" for i in x: \n" +
" print i, \n" +
" print x.__class__\n");
(from Bruce Eckel's 'Thinking in Patterns' chap 9, the 'Interpreter
Pattern'; worth downloading from
http://www.mindview.net/Books/TIPatterns/ )
or multi-line if/for statements.
But other than that you can't run multiple lines: that is what
'execfile' is for. Note that 'execfile' DOES take an InputStream so you
can send it code from any source.
Or just put 'exec' in a loop.
Of course 'eval' takes only an EXPRESSION (and returns a result...).
--
__o
Jim Adrig _ \<,_
ji...@tr... ' `/ ' `
___________ `-' `-'
"There are many ways of moving forward,
but only one way of standing still."
- Franklin D. Roosevelt
|