luerl icon indicating copy to clipboard operation
luerl copied to clipboard

Reading files

Open H2CO3 opened this issue 7 years ago • 10 comments

It does not appear to be possible to read files in any way. io.read doesn't exist, io.input doesn't exist, etc. Why? It's really annoying (also the fact that this is completely undocumented). loadfile, dofile, etc. don't work either, so they can't be used as a workaround (I want to read a config file). It's incredible that such a trivial task cannot be accomplished after several days of googling and trying to find any documentation on this issue -- without success.

H2CO3 avatar Jun 15 '17 09:06 H2CO3

You are the first to ask for it. I will see what I can do.

Btw we do have loadfile and dofile. They are in the list of currently supported Lua library functions listed in README.

rvirding avatar Jun 18 '17 20:06 rvirding

Yes, I tried both loadfile and dofile, but neither of them work for some reason. Whether I specify a relative or absolute path, with or without the .lua extension, etc. they can't find the file for some reason. (The file has got only ASCII printable characters in its name, it's not a Unicode normalization issue either.)

H2CO3 avatar Jun 19 '17 04:06 H2CO3

Could you give me an example?

rvirding avatar Jul 16 '17 17:07 rvirding

Sure, although I'm not sure how much additional information this provides, I'm afraid it's not too much. I'm using VerneMQ and I'm trying to read a configuration file like this:

return {
   db_engine              = "mysql",           -- mysql, postgresql, mongodb, redis, memcached
   pool_id                = "****",            -- whatever
   db_name                = "parashout",       -- database name
   msg_tbl_name           = "message",         -- table name in which messages are stored
   msg_id_col_name        = "id",              -- message primary key
   msg_type_col_name      = "type",            -- message type: text, image, seen
   msg_resend_id_col_name = "resend_id",       -- message resend ID
   msg_conv_id_col_name   = "conversation_id", -- ID of conversation message is sent to
   msg_sender_id_col_name = "sender_id",       -- user who sent the message
   msg_date_col_name      = "date",            -- date of sending
   msg_text_col_name      = "text",            -- if type == text, the text of the message, else NULL.
   msg_media_url_col_name = "media_url",       -- if type == zimage, the image URL, else NULL.
   host                   = "localhost",       -- DB connection host
   port                   = nil,               -- DB connection port
   username               = "verne",           -- DB connection username
   password               = "****",            -- DB connection password
}

In a VerneMQ plugin file, which is successfully run, I was trying to call:

local config = dofile("config.lua")

where config.lua is in the same directory as the plugin file.

This, however, failed: the VerneMQ error console said that dofile() can't find the file. Then I tried to specify the full path, like /home/verne/config.lua; this resulted in the same error. I have retried with loadfile() as well, with the same result.

H2CO3 avatar Jul 17 '17 11:07 H2CO3

This is the first I have heard of VerneMQ at all and that it uses luerl. :smile: I will try to check and see what is happening.

One thing is that dofile by default loads from the current working directory of the running erlang so there is no guarantee that it will automatically find the file in same directory as the plugin. I can load your config.lua so it can manage the file as such. My last question is whether the path is correct?

Can you also send me the plugin? If I still can't make head or tails of the problem I will contact the VerneMQ people myself and ask for help.

rvirding avatar Jul 19 '17 00:07 rvirding

@rvirding Thanks for your reply! Yes, I figured that the current directory is something related to the environment, that's why I tried with the absolute path as well. I assume the path should have been correct, because it was one of the first things that popped in my mind, so I checked it many times with e.g. cat and ls, and those did report a file at that location.

I'll have to check with my manager whether I am allowed to post the plugin – it's for an internal company project. I'll get back to you once I know that.

H2CO3 avatar Jul 19 '17 09:07 H2CO3

Hi, I am getting back into luerl now. Did you send me that plugin, I can't see it anywhere? How does VerneMQ use luerl?

rvirding avatar Mar 21 '18 00:03 rvirding

I tested this from the erlang shell putting the config in the file "v.lua" then doing

7> luerl:do("local local = dofile(\"v.lua\")", S0).

and it seemed to work.

rvirding avatar Mar 21 '18 01:03 rvirding

Hi, and thanks for getting back!

Unfortunately, the project has since been abandoned, and I no longer seem to be able to find the source for that plugin. 😞If it does (and is supposed to) work, then I can imagine it was only a problem with my setup…

Anyway, thanks for making luerl!

H2CO3 avatar Mar 21 '18 06:03 H2CO3

Hello, this issue was originally about reading files with io.read, which is still not implemented. Then discussion went to dofile and finally stopped. May I create a new one for io.read as feature request?

Kind regards, Michael.

micw avatar Oct 05 '20 05:10 micw