Such a simple question, but I have not found a reasonable answer to this.
I currently program in Python, an interpreted language. I always hear of people using multiple languages in the same program? Then I hear them using scripting languages; Can someone on here please in plain english explain to me the difference between what I know as programming languages and scripts, and how on Earth do people use multiple languages together. That just does not make any sense.
How can someone use Javascript, PHP, and ASP together. Or program a game in C/C++ and use python as scripts? It just doesn’t make sense to me, they are different languages for a reason, i assume, so how do they play a role with one another?
11
In many cases, when you see multiple languages used together, you will find that one is a compiled language and the other is a scripting language.
The compiled language is typically C/C++, but can be many other languages (Haskell, Erlang, Java, etc). The compiled language provides the base application. The base provides an interface to the underlying operating system as well as the foundation for the work to be done by the application. The application will often provide lower level functionality that makes it easier to develop using the scripting interface.
The scripting language is most often used to provide customization for the application. The customization can be part of the application provided by the vendor for their own convenience or the scripting interface can provide customization for the end user.
Many times, the script interface will provide almost a Domain Specific Language by providing useful functions and high level interface specific for the application, making it more easily customized.
A couple of well known examples. Emacs is written in C and customized using Emacs Lisp. TextMate is a compiled application customized using Ruby. Atom is a new text editor that is written in JavaScript to run on Node. Atom is more indirect. The application is (nearly?) all JavaScript while Node.js uses V8 which is written in C++. World of Warcraft has a scripting interface that uses Lua.
As a personal example, I worked on a brand-able installer where the base code was Objective-C/C++ in order to access UI elements on MacOS with customization using AppleScript. The AppleScript interface was used only by us in order to brand the application for each customer.
As others have said, use the right language for the right application.
3
Bill Door gave some good examples where the “main program” is written in C or C++, and a scripting language is included for customization, But there is also a common, but different scenario, where the “main program”, written in some “scripting language” (whatever people have in mind when they use that word) is extended by modules written in C or C++. For example, in Python, a typical use case would be to implement an extension or module in C, because of the better performance one can typically achieve this way. That’s why the core parts of Python modules like NumPy or SciPy are mostly written in C, or PyGame, or the Python Imaging Library (PIL).
1
Interoperability is usually achieved by transferring data
Generally, you can think each language being it’s own separate program. This is not always the case though…
Some examples:
- Sending sql queries to a database
- using json_encode() in php to encode a data structure into it’s string representation, which then can be decoded by java script.
- Exposing an api which can be called by an embedded interpreter. This api can be exposed by memory addresses (think lua tied in with c++). Another example is Jython, python that can be ran on the JVM.
Some reasons you want to do this:
- Some problems are easier to solve in other languages
- You can extend a binary program without having to re-compile it
- Tying in existing solutions so you don’t have to write stuff from scratch.