Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

it's scripts all the way down!

by QuillMeantTen (Friar)
on Mar 15, 2016 at 13:19 UTC ( [id://1157803]=perlquestion: print w/replies, xml ) Need Help??

QuillMeantTen has asked for the wisdom of the Perl Monks concerning the following question:

Greetings, fellow monks
I have been musing over the idea of learning a new programming language for about a week and I think I have settled on Lua.
Main reason is for scripting things such as Nmap or Snort, I know that many video games enable modding by embedding Lua but that's not my main use case.

After reading this I found some of my questions already answered, but here are others:
You might recall how I wondered, some time ago, about perl performance and how to improve it by rewriting parts of a perl script in C. Someone suggested to me the excellent FFI::Platypus but I mainly understood that before going down that road I might first want to be exhaustive in my benchmarking and profiling efforts, have a good look at my algorithms under the harshest light and THEN do it.

What I came here to ask today is kinda related:

  • What is to be gained from embedding lua inside a perl script?
  • Do lua coroutines provide anything useful when used in a dominantly Perl context?
    (Useful could be something to gain in maintainability, performance, scalability...)
  • Why isn't perl embedded in applications as often as lua is (especially since TinyPerl is the same size)?
    Thanks for your time reading me.

Replies are listed 'Best First'.
Re: it's scripts all the way down!
by perlfan (Vicar) on Mar 15, 2016 at 14:40 UTC
    What is to be gained from embedding lua inside a perl script?

    You can offer the users of your program a domain specific language (DSL); Lua is best know to provide support for this in particular. You mention Nmap and Snort, but mysqlproxy also uses it. It's used extensively to extend complex programs written in complex languages, e.g. C++ (WoW?). Redis is getting increasing support for Lua.

    Do lua coroutines provide anything useful when used in a dominantly Perl context? (Useful could be something to gain in maintainability, performance, scalability...)

    Sure, it can provide you with a way to manage many contexts by which you are doing work. Don't confuse this with multi-threading, but it can provide you with a cheap mechanism (i.e., context switching) to manage lots of unrelated blocks of work in the same execution thread. From what I've seen, I don't think Lua supports actual threads. http://www.qore.org is embeddable and supports threading, but I don't think it's as nice as Lua for this purpose.

    Why isn't perl embedded in applications as often as lua is (especially since TinyPerl is the same size)?

    Lua evolved to fit this explicit purpose. Perl is too heavy and doesn't provide the clean ways of binding a host program to the underlying Lua interpreter that make it a nice choice for building a DSL into your program.

    Update - you mention FFI::Platypus, but that's primarily for creating wrappers around shared libraries. It might help you avoid writing an XS module directly, but it's not going give you the solid platform for a DSL like Lua.

    PS: good reading on the history of Lua - http://www.lua.org/history.html

      Thank you for your reply,
      I found it to be most enlightening and took the time to read through your links and on DSLs.
      As I studied language theory and had to come up with a toy one I kinda related to different posts I found online warning people against DSLs.
      I have three follow up questions on that topic.

      • Beside the easily found example that I felt only scratched the surface, how spread is the use of DSLs?
      • Furthermore, when taking into account the difficulty of designing and maintaining a language (I found grammar and syntax to be complicated to do well, but it might just be me) why not use pure lua to script your application?
      • What are some good ressources on the most maintainable design patterns used when you need to come up with an architecture for supporting user written addons? (I read Module::Pluggable documentation's but I could not think of a way to make it able to directly use lua scripts without having to wrap each of them in a perl module

Re: it's scripts all the way down!
by perlfan (Vicar) on Mar 16, 2016 at 15:00 UTC
    Beside the easily found example that I felt only scratched the surface, how spread is the use of DSLs?

    A DSL is like things that are hard to define, but you know it when you see it. Perl has at least 2, regular expressions and XS. Any program that provides for language like constructs in their configurations or for which you can write "scripts". Matlab is a DSL for matrix operations. Erlang began as a prolog-based DSL. Ragel State Compiler uses a DSL. Some would call PHP a DSL for web applications. varnish cache has a DSL; httpproxy does...I'd say they're incredibly wide spread - some more functional than others. The key concept is that is tailored for a specific domain (application). I once wrote an XML based DSL for scripting the interaction of remotely connected computers over ssh. The engine was written in Perl.

    Furthermore, when taking into account the difficulty of designing and maintaining a language (I found grammar and syntax to be complicated to do well, but it might just be me) why not use pure lua to script your application?

    I've never embedded a scriptable interface, but the main language you use is for you; Lua is used to support a DSL that is tailored for your users. It's up to you, I've never seriously looked into doing something like this, so others might have a better opinion. Also, when using Lua to create a DSL, I think you're focused more on special functions rather than actual grammars. This is where Lua takes over; you just define and implement your special functions (and keywords?). E.g., Lua gives you "if" and other tradition language constructs.

    What are some good ressources on the most maintainable design patterns used when you need to come up with an architecture for supporting user written addons? (I read Module::Pluggable documentation's but I could not think of a way to make it able to directly use lua scripts without having to wrap each of them in a perl module

    Honestly, I'd look at the applications you mentioned before (Snort, Nmap); I'd look at mysqlproxy, varnish cache, Redis. I don't have any examples of a Perl application using Lua, but they've got to be out there.

    HTH

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1157803]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2024-03-28 11:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found