2

When I start an interactive Lua shell, io.write() adds unwanted material after the string I want it to print. print(), however does not:

[user@manjaro lua]$ lua
Lua 5.4.2  Copyright (C) 1994-2020 Lua.org, PUC-Rio
> io.write('hello world')
hello worldfile (0x7fcc979d4520)
> print('hello world')
hello world

And when I use io.write() in a program it works fine too:

--hello.lua
io.write('hello world\n')
print ('hello world')

Output:

[user@manjaro lua]$ lua hello.lua
hello world
hello world

I'm using Manjaro Linux on a Dell desktop. Can anyone tell me what's going on here? Thanks in advance.

EDIT: I should add, perhaps, that the unwanted material is always something like this:

file (0x7f346234d520)

It's always 'file' followed by what looks like a large hexadecimal number in parentheses. The exact number stays constant within one shell session but varies between different shell sessions.

  • `io.write` is equivalent to `io.open():write()` . `io.open()` returns a file handle (default output file). `file:write()` returns `file` on success so you ultimately see the string representation of that file handle after `hello world`. `print` does not return a value, hence you don't see any extra text. – Piglet Apr 10 '21 at 12:26

2 Answers2

3

"file (0x7fcc979d4520)" (or whatever address) is the return value of the io.write call, with an implicit tostring.

The lua(1) man page says

In interactive mode, lua prompts the user, reads lines from the standard input, and executes them as they are read. If the line contains an expression or list of expressions, then the line is evaluated and the results are printed.

The trouble here is that io.write('hello world') could be either an expression or a statement. Since it's a valid expression, the interpreter outputs that unwanted return value.

As a workaround, try adding a semicolon:

> io.write('hello world\n');
hello world    

Although Lua usually doesn't require a semicolon for each statement if it's at the end of a line, it does allow it. And important here, it means the syntax can't be an expression, only a statement which calls the function. So the interpreter won't output the returned value.

aschepler
  • 70,891
  • 9
  • 107
  • 161
  • Thank you. So this is regular behaviour? I'm only a beginner in Lua, but I could have sworn that this did not happen last time I used Lua interactive mode, a couple of days ago. And the official online manual contains `io.write()` statements without semi-colons that do not exhibit this behaviour: https://www.lua.org/pil/21.1.html. – Lewis Theobald Apr 10 '21 at 13:23
  • 1
    I don't really know. Maybe it's a difference in Lua versions. – aschepler Apr 10 '21 at 14:00
  • this probably depends on the operating system or the binaries used. I don't observe this under Windows with LuaDist for example. – Piglet Apr 10 '21 at 14:10
  • 1
    This behavior was introduced in Lua 5.3. – lhf Apr 11 '21 at 10:33
0

You are just seeing the return value of io.write when you call io.write manually, interactively. When using the Lua, uh, shell, if you want to call it that, it almost always prints the return value of any function(s) you call. file(blabblah) is the internal representation of the file you are writing to (probably just a hex memory address, but who knows?)

Walt Howard
  • 7,676
  • 1
  • 16
  • 11