lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Carol Hurwitz wrote:

> What is the difference between chunk and block
> grammatically?

A chunk is a whole file of Lua source code, a string passed
to loadstring, or one line (or multiline block) typed into
the command-line interpreter.  Blocks can be lexically
nested and share upvalues; chunks cannot.

You might have been confused by this statement in the
reference manual:

# syntactically, a block is equal to a chunk

This just means that this code

  print("Hello world!")

could be either a chunk or a block.  If it's the entire
contents of a file given as an argument to the interpreter,
then it's a chunk (and also a block, although one might be
less likely to call it that in this context); if it's in
between a "do" and an "end" (or other control structure
keywords) then it's not a chunk, but it's still a block.

> Somewhere it is stated that a #-line can be the first line
> in a chunk so that it can be run as a lua script.  The
> definition of block would mean that you could have such
> statements sprinkled throughout the the code, i.e.,
> wherever there is a block.

Nope, a "shebang" line only works as the first line of a
chunk as defined above.

> How do you do line continuation?  Do you use '\'?

Inside single-quoted or double-quoted strings, yes, although
the newline right after the backslash becomes part of the
string (if you don't want this, just use .. to concatenate
multiple strings together).  Otherwise, you can break lines
up almost any way you want:

  do

    print ( -- A function call open paren has to be on the
      -- same line as the function call itself.

      "Hello"

      ,

      "world!"

    )

  end


> Is there a nice little starter set of examples that I
> could look at?  Other than what is in the manual?

<http://lua-users.org/wiki/>

-- 
Aaron