Python/C++ vs. Perl/C++

assert

Perl has no built-in assert (this is embarrassing!), as do C++ and Python. assert greatly simplifies the writing of test code. Perl does offer a kludged-on assert, but it has several limitations, as demonstrated below.

Comment on this topic

Perl
require "assert.pl";

my $a = "MR";
my $b = "MR";
assert("$a eq $b");

$a = "MR.";
$b = "MR.";
#assert("$a eq $b"); # fails due to "syntax error"
assert("'$a' eq '$b'"); # Adding quotes works.

my %h = (1, "MR", 2, "MR");
assert("$h{1} eq $h{2}");

my %h1 = (1, "MR.", 2, "MR.");
#assert("$h1{1} eq $h1{2}"); # fails due to "syntax error"
assert("'$h1{1}' eq '$h1{2}'"); # works

my %h2 = ("1", "MR.", "2", "MR.");
assert("'$h2{\"1\"}' eq '$h2{\"2\"}'"); # works

$a = '';
$b = '';
assert("'' eq ''"); # works
#assert("$a eq $b"); # fails due to "syntax error"
assert("'$a' eq '$b'"); # Adding quotes works.

All kinds of obvious problems.

Python
a = "MR"
b = "MR"
assert(a == b)

a = "MR."
b = "MR."
assert(a == b) # works as is

h = {1:"MR", 2:"MR"}
assert(h[1] == h[2])

h1 = {1:"MR.", 2:"MR."}
assert(h1[1] == h1[2]) # works as is

h2 = {"1":"MR.", "2":"MR."}
assert(h2["1"] == h2["2"]) # works as is

a = ''
b = ''
assert(a == b) # works as is

The same code as above in Python. Works without special cases, and as expected.