dofile question

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

dofile question

Gregg Reynolds-2-2
Hi,

There's something I'm not understanding about dofile.  The manual says:
" Returns all values returned by the chunk", so I should be able to say

   x = dofile("foo.lua")

and if foo.lua contains e.g.

{
  a:sth{ ... }
  a:sth{ ... }
  ...
}

where a:sth returns a value, then x should be a table, no?  It should be
just like saying x = { a:sth{...} ... etc.

But no matter what I do with the contents of foo.lua, x ends up nil, so
I have to make a:sth populate a global table.

I've tried the following in foo.lua:

xx = { ...
xx{...
xx = { ... } return xx
return { ... }

etc., with functions appropriately defined, as far as I can tell.  No luck.

What am I missing?

-gregg
Reply | Threaded
Open this post in threaded view
|

Re: dofile question

Shmuel Zeigerman-2
Gregg Reynolds wrote:
>
> There's something I'm not understanding about dofile.  The manual says:
> " Returns all values returned by the chunk"
>
The chunk (the entire body of a file) is considered a function. So it
can explicitely return something, e.g.

-- foo.lua
-- some stuff
return 5, 6, "bar"
-- end of foo.lua

return doesn't have to be the last statement but it has to be
"top-level" that is, not inside some function.

--
Shmuel
Reply | Threaded
Open this post in threaded view
|

Re: dofile question

Gregg Reynolds-2-2
Shmuel Zeigerman wrote:
> Gregg Reynolds wrote:
>>
>> There's something I'm not understanding about dofile.  The manual says:
>> " Returns all values returned by the chunk"
>>
> The chunk (the entire body of a file) is considered a function. So it
> can explicitely return something, e.g.
>

Ok, so there must be a problem with using methods.  If my file says sth like

        return { x = 5 }
or
        return { "a", "b" }

then I get a table from dofile as expected.  But my file says sth like

        return {
          q:foo{ id='x1', ...}
          q:foo{ id='x2', ...}
        ...
        }

and when I run dofile, I get this sort of thing:

 test.ldf:13: '}' expected (to close '{' at line 1) near 'q'
stack traceback:
        [C]: in function 'dofile'
        stdin:1: in main chunk
        [C]: ?

Just to complicate things, if I have only a single q:foo{...} expression
in my file, then everything is copacetic - dofile returns the expected
table.  But as soon as I add another q:foo, I get the error.

I would expect an array holding the values returned by the q methods.
The object q is just a holder of methods, defined before I call dofile,
just so I can say q:foo etc. (think xml Qnames).  It works just fine
without the "return {...}" wrapper, so I don't think it's a syntax
problem.  Any ideas?  I'm flummoxed.

thanks,

gregg
Reply | Threaded
Open this post in threaded view
|

Re: dofile question

Javier Guerra Giraldez
On Sunday 23 April 2006 12:35 pm, Gregg Reynolds wrote:
> return {
>  q:foo{ id='x1', ...}
>  q:foo{ id='x2', ...}
> ...
> }

add a comma (',') between the array elements.

--
Javier

attachment0 (207 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: dofile question

Shmuel Zeigerman-2
In reply to this post by Gregg Reynolds-2-2
Gregg Reynolds wrote:
> return {
>  q:foo{ id='x1', ...}
>  q:foo{ id='x2', ...}
> ...
> }

It seems you're getting a syntax error since there must be commas in
between the method calls:

  return {
   q:foo{ id='x1', ...}, --> comma
   q:foo{ id='x2', ...}
        ...
  }

--
Shmuel
Reply | Threaded
Open this post in threaded view
|

Re: dofile question

Gregg Reynolds-2-2
Shmuel Zeigerman wrote:
...
> It seems you're getting a syntax error since there must be commas in
> between the method calls:
>

(**red-face**)

thanks
Reply | Threaded
Open this post in threaded view
|

Re: Correction about `return' usage

Shmuel Zeigerman-2
In reply to this post by Shmuel Zeigerman-2
Tomas wrote:
> On Sun, 23 Apr 2006, Shmuel Zeigerman wrote:
>
>> return doesn't have to be the last statement but it has to be
>> "top-level" that is, not inside some function.
>
>     `return' has to be the last statement.  For example:
>
>> f=assert(loadstring"return 1; print(2)")
[snip]

Tom,
I meant "return doesn't have to be the last statement in the file". It's
my fault I didn't write that explicitely (I thought it was clear in the
context).

--
Shmuel