Scripting Language Constructs

One way scripting languages differ from system languages is that they greatly emphasize ease of writing over performance. So you tend to see a few constructs you'll probably never see in C.

Here are some features that tend to appear in scripting languages. Not all of these features are in every scripting language. And some of these features exist in non-scripting languages, too.

System and Environment Access

Many scripting languages allow direct access to system commands and environment variables.

System commands

Perl has the following "system commands" as top-level functions: chmod, chown, link, stat, mkdir, rename, rmdir, stat, symlink, ...

TODO

Environment Variables

TODO

File Test Operators

The following Perl unary operators take in either a filename or a filehandle. (If the argument is omitted, it is taken to be $_, or STDIN for -t).

-r -w -x -o
-R -W -X -O
-e
-z
-s
-f -d
-l -S -p
-b -c
-u -g -k
-t
-T -B
-M -A -C

Data Structure Literals and Operators

If you've ever tried to write down a simple list or dictionary in C or Java, you'll love these.

Arrays and Lists

Slices

List Comprehensions

>>> a = [1, 2, 10, 6]
>>> b = [3, 0, 4, -8]
>>> [4 * x for x in a]
[4, 8, 40, 24]
>>> [2 / x + 5 for x in a if x < 4]
[7, 6]
>>> [x + y for x in a for y in b]
[4, 1, 5, -7, 5, 2, 6, -6, 13, 10, 14, 2, 9, 6, 10, -2]
>>> [(x, x**3) for x in a]
[(1, 1), (2, 8), (10, 1000), (6, 216)]
>>> [x + y for x in a for y in b if y <= 0]
[1, -7, 2, -6, 10, 2, 6, -2]
>>> [a[i] * b[i] for i in range(len(a))]
[3, 0, 40, -48]
>>> [str(round(355/113.0, i)) for i in range(1,6)]
['3.1', '3.14', '3.142', '3.1416', '3.14159']
>>> [(i, 2**i) for i in range(10)]
[(0, 1), (1, 2), (2, 4), (3, 8), (4, 16), (5, 32), (6, 64), (7, 128), (8, 256), (9, 512)]
>>> [x for x in [y*y for y in range(10)] if x % 2 == 0]
[0, 4, 16, 36, 64]
>>> names = ["ALICE", "BOb", "cAROl", "daVE"]
>>> [(n.lower(), n.upper()) for n in names]
[('alice', 'ALICE'), ('bob', 'BOB'), ('carol', 'CAROL'), ('dave', 'DAVE')]
>>> [i for i in range(10,30) if i not in range(5, 40, 2)]
[10, 12, 14, 16, 18, 20, 22, 24, 26, 28]
>>> [(a,b,c) for c in range(1,30) for b in range(1,c) for a in range(1,b) if a*a+b*b==c*c]
[(3, 4, 5), (6, 8, 10), (5, 12, 13), (9, 12, 15), (8, 15, 17),
(12, 16, 20), (15, 20, 25), (7, 24, 25), (10, 24, 26), (20, 21, 29)]

Python list comprehensions are

Dictionaries

Iterators

Automatic Garbage Collection

Regular Expressions

Most scripting languages have a built-in syntax for regular expressions.

Flexible Function Declarations

Functions with variable or default arguments

Anonymous Functions

# In Perl
sub {$_[0] + 2 * $_[1];}

# In JavaScript
function(x, y) {return x + 2 * y;}

# In Python
lambda x, y: x + 2 * y

# In Ruby, we have proc objects, which aren't exactly
# functions.  You have to use the call method of the
# proc to invoke it.
proc {|x, y| x + 2 * y}

Code Objects

Higher Order Functions

Closures

Ruby Blocks

Flexible Classes and Objects

Processes and Threads

Dynamic Language Features

Most scripting languages contain dynamic language features which are covered elsewhere.