Announcing Synergy/DE 12.3.1.2023, new LTS update
April 15, 2025Overview of scripting languages
Statically typed compiled languages, such as traditional Synergy and Synergy .NET, are great for large, structured applications meant to withstand the test of time. Programs come in all shapes and sizes, though. For smaller programs meant for a specific task, or programs only intended to run once, a scripting language is often more appropriate. At Synergex, we primarily use Perl, Python, and PowerShell for these purposes.
What are the benefits of using a scripting language? There are several:
- Scripting languages don’t require compiling or linking, which allows code to be tested more quickly and also avoids issues that can occur when a developer makes a change to the source but forgets to compile.
- Scripting languages often support a REPL, or read-evaluate-print loop. A REPL allows you to type an expression and immediately see the computed result, as well as save it in a variable for future computations. Python already comes with a REPL. The PowerShell console offers similar functionality in that you can directly use expressions and variables. You can simulate a REPL in Perl by invoking the Perl command-line debugger.
- Scripting languages are usually dynamically typed, which means you save time by not having to write type declarations for each variable. Object fields are more flexible in dynamic languages, often allowing a new member variable to be added to a particular object at runtime without needing to change a class definition.
You can use these languages for a variety of purposes. At Synergex, we use Perl extensively for testing the Synergy compiler and runtime. PowerShell is helpful when batch scripts would be too unwieldy, and it is also an important part of CI/CD pipelines for Windows machines. Python is used for various tasks, such as processing grammar files related to the Synergy compiler, automating builds, and experimenting with AI technologies. Scripting languages are also popular choices when spinning up a local web server.
Which language should you use? It depends on what you prefer and what your team already knows. If most developers in your company know a particular language already, choose that one. Otherwise, look at the features of each language and figure out what suits your needs. For instance, although Perl and Python are both dynamic general-purpose languages, their design is very different. In Python, the + operator is used for numeric addition, string concatenation, and list concatenation. In Perl, + only performs numeric addition, . (dot operator) only does string concatenation, and array concatenation can be achieved using the push function or merging arrays such as (@array1, @array2). It may be surprising that 1+1 and “1”+”1″ are both 2 in Perl; in the second case, both strings are converted to numbers before the addition occurs. Since the dot operator always concatenates, 1 . 1 and “1”.”1″ are both the string “11”. Be careful with whitespace around the dot, as 1.1 is a floating-point literal. In Python, the operator chooses which action to perform depending on the type of the arguments. In Perl, it’s the exact opposite: the arguments choose how to convert themselves depending on the type of the operator. Perl offers many ways to write the same piece of code, while Python’s philosophy is to focus on clear syntax and readability.
Don’t feel limited to the three languages already mentioned: there are plenty of other options. Ruby is a scripting language that was designed with object-oriented features from the beginning, which may appeal to users who prefer OOP to be a fundamental part of the language. Lua is a minimalistic scripting language that can easily be embedded into C applications. If your business has a significant number of web developers, they will be comfortable with JavaScript, and you can use Node.js to run JavaScript outside the browser for desktop development and server-side scripting.
Perl tips
Since Perl has a long legacy at Synergex, here are some quick Perl tips that you may find helpful.
1. Quoting
In Perl, there isn’t a character data type; rather, you use strings of length 1 to represent characters. Both single quotes and double quotes can contain multiple-character strings, like in Synergy DBL. In Perl however, double quoted strings are special in that they process variable interpolation and escape sequences, while single quotes don’t. Here’s a short program:
my $world = 'WORLD';
print "Hello $world\n";
print 'Hello $world\n';
The second and third lines differ only by double quotes versus single quotes. The first interpolates the variable $world and has a newline character at the end, printing Hello WORLD. The second does neither, printing Hello $world\n with a literal dollar sign and backslash.
This means that if your string has no variables or escape sequences, it is slightly more performant to use single quotes, because the Perl interpreter doesn’t have to scan the string for interpolation purposes.
Perl also has useful quoting operators. These two list expressions are equivalent:
('one', 'two', 'three', 'four')
qw(one two three four)
The qw() operator automatically splits on whitespace and doesn’t need all those quotation marks.
2. Keywords unless and until
Every developer knows about if statements and while loops, but Perl also has unless statements and until loops. Unless just means “if not” and until means “while not.” If you have a condition where the entire expression is wrapped in parentheses and preceded by a “not” operator, such as
!($a && $b && $c)
consider using
unless ($a && $b && $c)
instead of
if (!($a && $b && $c))
to remove one layer of parentheses.
Use unless/until sparingly and only when it makes the condition clearer to read. When in doubt, use traditional if/while.
3. Multi-line comments
Unlike languages like C, Perl doesn’t use /* and */ for block comments. You can start a block comment with =pod and end it with =cut, each on its own line. The syntax seems weird, but =pod stands for “plain old documentation.” Although intended for documentation, you can use this syntax for regular comments too.
4. Assign all arguments
You can access subroutine arguments by number, using $_[0], $_[1], $_[2], etc. To give meaningful names to all these arguments in a single line of code, put the array of all arguments on the right side of an assignment and the list of variable names on the left.
my ($arg1, $arg2, $arg3) = @_;
5. Functions map and grep
The grep function isn’t just for regular expressions. If you’ve used LINQ in Synergy .NET or C#, you’ll know about Select() and Where(). Select() transforms a sequence, and Where() can filter it. In Perl, these are called map and grep, respectively. You can create short one-line pieces of code that perform the actions of multiple foreach loops. Here’s how to multiply all numbers in a list by 2, then throw away the ones that are divisible by 10. Notice that $_ refers to the default argument.
my @integers = (1,2,3,4,5,6,7,8,9,10,11,12);
my @evens = grep { ($_ % 10) != 0 } map { 2 * $_ } @integers;
Links
Here are the links for the programming languages mentioned in this article.
Synergy/DE documentation: https://www.synergex.com/docs/
Perl: https://www.perl.org/
Python: https://www.python.org/
PowerShell: https://learn.microsoft.com/en-us/powershell/
Ruby: https://www.ruby-lang.org/en/
Lua: https://www.lua.org/
Node.js: https://nodejs.org/en