Why not Just PHP-stupid include?

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

Why not Just PHP-stupid include?

Patrick Mc(avery-2
I'm apologizing in advance for the first negative toned email I have
sent to this list.

I really respect Luiz and Roberto and there have been a lot of helpful
people on this list but Lua's glow is dimming for me. It's tiresome
watching this endless battle between Lua titans over module while
lua-peasants like me seem to be without basic tools.

I've been on the list for about 2 years, read PIL cover-to-cover >5X
times bought all the other Lua books yet I had to ask another list
member how to include code without module or dofile. I think every
newcomer to the language would expect this to work:

(file oneVar.lua)
local x = 1

(file simpleTest.lua)
print("starting")
dofile("oneVar.lua")
print(x)
print("the end")

Yet the results are:

(results)
starting
nil
the end

I don't want to have to pack x into a table and return it, on the other
end I don't want to have to define a new local variable to receive the
payload from require. PHP is a poorly constructed language but include
just switches execution from one file to the other and then goes back to
the first, it's dead simple to pick this up. Please consider the little
people down here that don't have code that will be published-Patrick

Reply | Threaded
Open this post in threaded view
|

Re: Why not Just PHP-stupid include?

Florian Weimer
* Patrick Mc:

> I don't want to have to pack x into a table and return it, on the
> other end I don't want to have to define a new local variable to
> receive the payload from require. PHP is a poorly constructed language
> but include just switches execution from one file to the other and
> then goes back to the first, it's dead simple to pick this up.

Both things are not totally unrelated, unfortunately.  Lua has proper
lexical scoping, and each file ends up in its own chunk.  So when you
write

    local x = 1

in one file, it only affects that scope.  It works as expected if you
drop the "local".

I think it will be possible in Lua 5.2 to use the "debug" library
("upvaluejoin" in particular) to obtain the effect you desire.
I'm not sure if that's a good idea, though.

Reply | Threaded
Open this post in threaded view
|

Re: Why not Just PHP-stupid include?

Patrick Mc(avery-2
{ snip }

> Both things are not totally unrelated, unfortunately.  Lua has proper
> lexical scoping, and each file ends up in its own chunk.  So when you
> write
>
>      local x = 1
>
> in one file, it only affects that scope.  It works as expected if you
> drop the "local".
>
> I think it will be possible in Lua 5.2 to use the "debug" library
> ("upvaluejoin" in particular) to obtain the effect you desire.
> I'm not sure if that's a good idea, though.
>
>
Thanks Florian

That's a good point about local, in another file it means local to that
file.

List,

This problem with sharing code seems very complex and I am sure due to
the fact that it must include C modules too it is not easy at all.

Having said this the language is billed as being suitable for
non-programmers. Please see page 156 of PIL:

""typically developed by one or a few programmers or even by
non-programmers""

Since this is the case I think mechanisms for non-programmers should be
of high importance. Module/require/dofile are very subtle and confusing,
PHP-dumb include is not.

All language features do not have to be used by all, why not just add a
php-dumb-include that overrides local, as in local to that file for
non-programmers and bill the other mechanisms as to be used by advanced
users?

Reply | Threaded
Open this post in threaded view
|

Re: Why not Just PHP-stupid include?

Alex Queiroz
Hallo,

On Tue, Oct 18, 2011 at 2:42 PM, Patrick Mc(avery
<[hidden email]> wrote:
>
> Since this is the case I think mechanisms for non-programmers should be of
> high importance. Module/require/dofile are very subtle and confusing,
> PHP-dumb include is not.
>

     Simple is not a synonym to dumb. The worst thing that could
happen to Lua is become more similar to PHP.

--
-alex
http://www.artisancoder.com/

Reply | Threaded
Open this post in threaded view
|

Re: Why not Just PHP-stupid include?

Daurnimator
In reply to this post by Patrick Mc(avery-2
On 18 October 2011 23:42, Patrick Mc(avery
<[hidden email]> wrote:
> why not just add a
> php-dumb-include that overrides local, as in local to that file for
> non-programmers and bill the other mechanisms as to be used by advanced
> users?
>
>

Because the reason to use local is for when you don't want anyone else
to be able to see the variable:
that is the whole point of local; and to remove that... why are you
using local in the first place??????

In this way, its like a global 'static' in a C file.

Reply | Threaded
Open this post in threaded view
|

Re: Why not Just PHP-stupid include?

Patrick Mc(avery-2
{snip}
> Because the reason to use local is for when you don't want anyone else
> to be able to see the variable:
> that is the whole point of local; and to remove that... why are you
> using local in the first place??????
>
> In this way, its like a global 'static' in a C file.
>

I do like locals but local to a do-end blocks or functions not necessary
files. So here is a simple use case.

Let's say I define 1 function and put it in a file called function.lua,
it looks something like this:
function silly1() print "reallly long string that greets user" end

and define 1 local variable
local x = 10
and put it in another file called local.lua.

I could then write code in another file that would patch the two
together something like this:
  function sillyCode()
include("functions.lua)
silly1()
local y =2
include("locals.lua")
print(x + y)
end

the include file is just a way to store code somewhere else. I have
download code from luaforge that had 3K lines in one file, this is
weird. Other languages have this simple-PHP like mechanism, it's not
about encapsulation it's just about typing. This example was short but
why could someone not define 100 local variables and then when the
include is called from within a function, those locals become local to
that function as if they were typed inside it.



Reply | Threaded
Open this post in threaded view
|

Re: Why not Just PHP-stupid include?

steve donovan
On Tue, Oct 18, 2011 at 3:16 PM, Patrick Mc(avery
<[hidden email]> wrote:
> I could then write code in another file that would patch the two together
> something like this:
>  function sillyCode()
> include("functions.lua)
> silly1()
> local y =2
> include("locals.lua")
> print(x + y)
> end

Ah, you want something that works like C's #include - that is, _just
pulls code into the current file_.

I see sometimes a need for it, e.g. if you have a lot of local aliases
for global functions, but the Lua way here is to _separately compile_
each file brought in with dofile() or require().

Generally, history has not been kind to plain include - it's the major
boneheaded thing about C (hi, Petite!) which came to its full horrible
fruition with C++.  The poor compiler (after all) still has to process
all this included stuff to compile the file, so the C++ compiler goes
through multiple 10KLocs of source to compile a modern "hello word' .

steve d.

Reply | Threaded
Open this post in threaded view
|

Re: Why not Just PHP-stupid include?

Jerome Vuarand
In reply to this post by Patrick Mc(avery-2
2011/10/18 Patrick Mc(avery <[hidden email]>:
> [...]
>
> I don't want to have to [...], on the other end I don't want to have to [...]

Basically, you want to do something, there is a way to do it in Lua,
but you don't *want* to do it that way. I agree it would be nice if
the Lua authors did realize every one of our wishes, but given the
size of the community, it's impossible (because we have conflicting
wishes). I don't want to defend the status quo and prevent any change
in the language, but the reasons you give about the superiority of the
PHP (basically its simplicity) are very subjective.

> PHP is a poorly constructed language but include just switches
> execution from one file to the other and then goes back to the first

Isn't PHP a poorly constructed language precisely because of such
mis-features? As others have pointed that feature would mess the
beautiful consistency of Lua in respect to scoping rules.

2011/10/18 Patrick Mc(avery <[hidden email]>:
> [...] Other
> languages have this simple-PHP like mechanism, it's not about encapsulation
> it's just about typing. This example was short but why could someone not
> define 100 local variables and then when the include is called from within a
> function, those locals become local to that function as if they were typed
> inside it.

Some languages, like C or C++, use a preprocessor to implement such
features. The compiler itself takes its translations units whole and
consistent, just like Lua. I believe you can use existing
preprocessors (m4, gcc -E, etc.) and apply them to Lua source files.

Reply | Threaded
Open this post in threaded view
|

RE: Why not Just PHP-stupid include?

Julien Duminil
In reply to this post by Patrick Mc(avery-2
> I do like locals but local to a do-end blocks or functions not
> necessary files. So here is a simple use case.

Let's see again your first example:

> (file oneVar.lua)
> local x = 1
>
> (file simpleTest.lua)
> print("starting")
> dofile("oneVar.lua")
> print(x)
> print("the end")

You have to see dofile("oneVar.lua") as:

do
    php_dumb_include("oneVar.lua")
end

Also, dofile is done at runtime, not at compile time, so when you load simpleTest.lua, the compiler don't know that there is a local variable x defined in oneVar.lua.

Julien D.


Reply | Threaded
Open this post in threaded view
|

Re: Why not Just PHP-stupid include?

Patrick Mc(avery-2
In reply to this post by Jerome Vuarand
[snip]

>> [...] Other
>> languages have this simple-PHP like mechanism, it's not about encapsulation
>> it's just about typing. This example was short but why could someone not
>> define 100 local variables and then when the include is called from within a
>> function, those locals become local to that function as if they were typed
>> inside it.
> Some languages, like C or C++, use a preprocessor to implement such
> features. The compiler itself takes its translations units whole and
> consistent, just like Lua. I believe you can use existing
> preprocessors (m4, gcc -E, etc.) and apply them to Lua source files.
>
>
Hi Steve

Yes a bit like C include but not, this still has a scope modification.
We can define scope with do-end so I was hoping for something that had
no scope affect.

Hi Jerome


I haven't coded in PHP since finding Lua 2 years ago. It really does
seem like a piece of crap now but I love the screw-CS and make it work
for regular people approach. I can create a preprocesser now but I sure
as hell could not do this when I first meant lua. The language is
supposed to be for non-programmers but the all of the discussions about
scope fly over the needs of the non-programmer who is left with an odd
development environment where code is not easy to split into separate
files without great controversy or tediousness.  If there is a conflict
in goals should not the non-programmers win out over academic high level
programming?


Reply | Threaded
Open this post in threaded view
|

Re: Why not Just PHP-stupid include?

Andre Leiradella-2
In reply to this post by Patrick Mc(avery-2
> Let's say I define 1 function and put it in a file called function.lua,
> it looks something like this:
>
> function silly1() print "reallly long string that greets user" end
>
> and define 1 local variable

> local x = 10

> and put it in another file called local.lua.

You do realize that in your example x is local but silly1 is global, right?

Why don't you declare x as a global? It seems to me that it's what you want.

Cheers,

Andre



Reply | Threaded
Open this post in threaded view
|

Re: Why not Just PHP-stupid include?

Patrick Mc(avery-2
On 11-10-18 10:01 AM, [hidden email] wrote:

>> Let's say I define 1 function and put it in a file called function.lua,
>> it looks something like this:
>>
>> function silly1() print "reallly long string that greets user" end
>>
>> and define 1 local variable
>> local x = 10
>> and put it in another file called local.lua.
> You do realize that in your example x is local but silly1 is global, right?
>
> Why don't you declare x as a global? It seems to me that it's what you want.
>
> Cheers,
>
> Andre
>
>
>
>
Hi Andre

No I meant them both to be local, it's not real code for anything I want
to do. I am just trying to say that if I type local x = 1 in another
file and then include it, I would like it become local to where it was
included from and not the file it was defined in.

List,

I mentioned a function called copy_in in another post and had no
replies, likely due to the stupidity of the name. However a function
name that described something like this could be used in tandem with
require/dofile to break a bigger problem into smaller chunks, by giving
different users what they need and again I think that non-programmers
should be first in line.

Alternatively perhaps a different file extension would help, a kind of
lua header that would define code that is essentially copied in verbatim
with no scope effect.

Thanks

Reply | Threaded
Open this post in threaded view
|

Re: Why not Just PHP-stupid include?

Joshua Jensen-2
----- Original Message -----
From: Patrick Mc(avery
Date: 10/18/2011 8:42 AM

> On 11-10-18 10:01 AM, [hidden email] wrote:
>>> Let's say I define 1 function and put it in a file called function.lua,
>>> it looks something like this:
>>>
>>> function silly1() print "reallly long string that greets user" end
>>>
>>> and define 1 local variable
>>> local x = 10
>>> and put it in another file called local.lua.
>> You do realize that in your example x is local but silly1 is global,
>> right?
>>
>> Why don't you declare x as a global? It seems to me that it's what
>> you want.
>>
>>
>
> No I meant them both to be local, it's not real code for anything I
> want to do. I am just trying to say that if I type local x = 1 in
> another file and then include it, I would like it become local to
> where it was included from and not the file it was defined in.
FWIW, I was asking for something similar in [1].

[1] - http://lua-users.org/lists/lua-l/2011-08/msg00910.html

-Josh

Reply | Threaded
Open this post in threaded view
|

Re: Why not Just PHP-stupid include?

Axel Kittenberger
In reply to this post by Patrick Mc(avery-2
On Tue, Oct 18, 2011 at 2:11 PM, Patrick Mc(avery
<[hidden email]> wrote:
> I'm apologizing in advance for the first negative toned email I have sent to
> this list.
>
> I really respect Luiz and Roberto and there have been a lot of helpful
> people on this list but Lua's glow is dimming for me. It's tiresome watching
> this endless battle between Lua titans over module while lua-peasants like
> me seem to be without basic tools.

I feel the same kind head-shaking emotion skimming through all this
bickering of doing something well as simple as modules. And yes the
globals issue with old modules is bad. A simple-stupid include is also
not good. I dunno, why can node.js do  it right on modules so simply,
albeit in general Lua would be a much saner language than javascript.
I'm happy that in my Lua project I just don't use any modules or
support them.

Reply | Threaded
Open this post in threaded view
|

Re: Why not Just PHP-stupid include?

Joshua Jensen-2
In reply to this post by Jerome Vuarand
----- Original Message -----
From: Jerome Vuarand
Date: 10/18/2011 7:25 AM
> Some languages, like C or C++, use a preprocessor to implement such
> features. The compiler itself takes its translations units whole and
> consistent, just like Lua. I believe you can use existing
> preprocessors (m4, gcc -E, etc.) and apply them to Lua source files.
I wrapped the mcpp preprocessor [1] as a Lua module.  You can find it
here [2].

An example:

function preprocess(filename, defines, includes)
     require 'mcpp'

     local mcppInput = {}
     mcppInput[#mcppInput + 1] = "-P"
     mcppInput[#mcppInput + 1] = filename

     for _, define in ipairs(defines) do
         mcppInput[#mcppInput + 1] = "-D"
         mcppInput[#mcppInput + 1] = define
     end

     for _, includeDirectory in ipairs(includes) do
         mcppInput[#mcppInput + 1] = "-I"
         mcppInput[#mcppInput + 1] = includeDirectory
     end

     return mcpp.preprocess(mcppInput)
end

-Josh

[1] - http://mcpp.sourceforge.net/
[2] - https://github.com/jjensen/luaplus51-all/tree/master/Src/Modules/mcpp

Reply | Threaded
Open this post in threaded view
|

Re: Why not Just PHP-stupid include?

Tony Finch
In reply to this post by Axel Kittenberger
Axel Kittenberger <[hidden email]> wrote:
>
> I dunno, why can node.js do  it right on modules so simply,
> albeit in general Lua would be a much saner language than javascript.

There's not much difference between the require functions in Lua and Node.

The search paths work differently.

Node has a magic "exports" object whereas Lua lets the programmer choose
the name of the exported table which must be explicitly returned.

Node and Lua-5.1 suppress recursive requires. Lua-5.2 does not.

The differences in scoping seem to have more to do with differences
between JavaScript and Lua rather than anything to do with modules per se.

Tony.
--
f.anthony.n.finch  <[hidden email]>  http://dotat.at/
Sole: Northwesterly 4 or 5, occasionally 6 in east. Rough or very rough. Rain
then showers. Good occasionally moderate.

Reply | Threaded
Open this post in threaded view
|

Re: Why not Just PHP-stupid include?

Andre Leiradella-2
In reply to this post by Patrick Mc(avery-2
> > I really respect Luiz and Roberto and there have been a lot of helpful
> > people on this list but Lua's glow is dimming for me. It's tiresome watching
> > this endless battle between Lua titans over module while lua-peasants like
> > me seem to be without basic tools.

> I feel the same kind head-shaking emotion skimming through all this
> bickering of doing something well as simple as modules. And yes the
> globals issue with old modules is bad. A simple-stupid include is also
> not good. I dunno, why can node.js do it right on modules so simply,
> albeit in general Lua would be a much saner language than javascript.
> I'm happy that in my Lua project I just don't use any modules or
> support them.

I feel the whole thing doesn't belong to Lua. Lua *was* simple to embed when all you had to do was statically compile libraries along with your program and luaopen_<libname> them. Yeah, this is still possible but one has to massage things first.

I'm not against making Lua more accessible, but for me it's things like LuaForWindows that make it happen, not modules. And yes, LfW uses modules, but just because that's how 3rd party libraries are distributed now. LfW could just statically compile everything together instead.

Cheers,

Andre



Reply | Threaded
Open this post in threaded view
|

Re: Why not Just PHP-stupid include?

Georg Lehner
In reply to this post by Patrick Mc(avery-2
Hi!

Maybe a stupid idea, but you could concatenate all code strings and then
"loadstring(strings)()" them?

- - -
loadstring([[
print("starting")
]]
.. io.input("oneVar.lu"):read("*a")
.. [[
print(x)
print("the end")
]])()
- - -

Performance issues put apart.  This can be syntaxsugarified with args
and an "include(filename)" function instead of io.input...

Regards,

     Jorge-León


On 10/18/11 14:11, Patrick Mc(avery wrote:

> I'm apologizing in advance for the first negative toned email I have
> sent to this list.
>
> I really respect Luiz and Roberto and there have been a lot of helpful
> people on this list but Lua's glow is dimming for me. It's tiresome
> watching this endless battle between Lua titans over module while
> lua-peasants like me seem to be without basic tools.
>
> I've been on the list for about 2 years, read PIL cover-to-cover >5X
> times bought all the other Lua books yet I had to ask another list
> member how to include code without module or dofile. I think every
> newcomer to the language would expect this to work:
>
> (file oneVar.lua)
> local x = 1
>
> (file simpleTest.lua)
> print("starting")
> dofile("oneVar.lua")
> print(x)
> print("the end")
>
> Yet the results are:
>
> (results)
> starting
> nil
> the end
>
> I don't want to have to pack x into a table and return it, on the
> other end I don't want to have to define a new local variable to
> receive the payload from require. PHP is a poorly constructed language
> but include just switches execution from one file to the other and
> then goes back to the first, it's dead simple to pick this up. Please
> consider the little people down here that don't have code that will be
> published-Patrick
>


Reply | Threaded
Open this post in threaded view
|

Re: Why not Just PHP-stupid include?

Luiz Henrique de Figueiredo
In reply to this post by Patrick Mc(avery-2
> I could then write code in another file that would patch the two
> together something like this:
>  function sillyCode()
> include("functions.lua)
> silly1()
> local y =2
> include("locals.lua")
> print(x + y)
> end

You could write a simple reader function that reads input line by line and
understands includes but it will be fooled by long strings and long comments.

Reply | Threaded
Open this post in threaded view
|

Re: Why not Just PHP-stupid include?

Josh Simmons
I'm not sure I see any advantage in regressing to a text based
include. Loading external code in Lua is simple and all external code
is handled the same way whether it comes from a string or a file or
puff the magic dragon. If this were to be implemented you'd then have
two different systems (run-time and compile-time) working on the same
problem for little benefit. I certainly don't think this would make
anything easier for anybody.

12