Scripting languages are found all over your Linux system. From Python to Shell, from Ruby to Haskell, there are so many flavours of scripting language that to choose just one would be the height of boring distaste. Over the coming few articles, I shall endeavour to show you the basics of a number of scripting languages, but in order to keep things short and sweet, I shall limit myself to three languages - that of Lua, Perl, and Python.
Scripting languages differ from compiled languages in a variety of ways which we have discussed before, but the critical aspect we're going to discuss today is called a REPL.
A REPL (Read-Evaluate-Print-Loop) is essentially exactly what the shell is. It is a style of user interface where a program prompts for input, reads that in, evaluates it there and then, prints the result out, and then loops back to prompting for new input. Each of Lua, and Python have REPLs; where Perl simply reads a program from standard input when you run the language interpreter with no other arguments.
Lua's REPL looks like this:
Lua 5.3.1 Copyright (C) 1994-2015 Lua.org, PUC-Rio
>
Python's more like this:
Python 3.4.2 (default, Oct 8 2014, 10:45:20)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
However, since Perl has all the tools known to man, if you install
Devel::REPL
(the libdevel-repl-perl
package on Debian and derivatives) you
can simply run re.pl
and get the following very perly prompt:
$_
In all three cases, pressing Control+D will exit the repl (providing you enter that on an otherwise blank input line), but you can also use (for Lua):
> os.exit(0)
for Python:
>>> import sys
>>> sys.exit(0)
for Perl:
$_ exit 0
Now, despite their various roots, scripting languages often share some very simple syntax between them. Though depending on the origin of the language the specifics might vary. For today we're just going to look at the simplest of commands -- writing some output to stdout.
For Lua, there is a function called print
which takes any number of arguments
and writes them to stdout with a newline at the end:
> print("Hello World", 1+5)
Hello World 6
>
Lua's print
function also adds tabs between the values you give it. In
Python 2, print
is a statement not a function, though that changes with
Python 3. Despite this, Python also adds a newline:
>>> print "Hello World", 1+5
Hello World 6
>>>
As you can see, Python added a single space rather than a tab. Perl's a little
more text-oriented by default though, and its print
statement does not append
a newline by default, nor does it insert any spaces or tabs, so we need to do:
$_ print "Hello World ", 1+5, "\n"
HelloWorld6
$_
For this week, I ask that you pick a scripting langauge, either one of the
above or any other you fancy, and spend some time getting used to the REPL
for it. Practice using the print
statement (or equivalent in your chosen
language) with strings and simple expressions. Get comfortable with it and
next time we'll look at some basic data storage.