Skip to Content
Lua Quick Start Guide
book

Lua Quick Start Guide

by Gabor Szauer
July 2018
Beginner
202 pages
5h 42m
English
Packt Publishing
Content preview from Lua Quick Start Guide

Scope access

Chunks are all about scope! You can access any variable defined outside of a scope from within the scope. Think of a scope like a one-way window in a room; from the inside you can see out, but from the outside you can't see in:

foo = 7 -- global scopedo    local bar = 8 -- local scope    print ("foo: " .. foo)    print ("bar: " .. bar)end

However, you can't access a variable local to a scope outside of that scope:

foo = 7 -- globaldo    local bar = 8 -- localendprint ("foo: " .. foo)print ("bar: " .. bar) -- error!-- bar was declared local to the do/end chunk-- it is trying to be printed at the file or-- global chunk level, where it does not exist

The same access pattern is also true for multiple nested chunks:

foo = 7 -- globaldo local ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Beginning Lua Programming

Beginning Lua Programming

Kurt Jung, Aaron Brown
Vim Masterclass

Vim Masterclass

Jason Cannon

Publisher Resources

ISBN: 9781789343229Supplemental Content