I'm running Perl scripts version 5.28 under Linux Debian with some experimental "extensions" and got an strange behavior. In a mixed Pascal-SQL-Perl programming session, I miss typed a lowercase begin
instead of an else
in an if-else-statement (see test code below) and got an valid but malfunctioning code.
I'm aware that the uppercase BEGIN
keyword belongs to BEGIN, UNITCHECK, CHECK, INIT and END
program "pre- and post-program-running" group. But I used the lowercase begin
variant and I got a valid running code. It seems to me, that the begin {...}
block is executed separately after the if (...) {...}
expression, which causes an unwanted early return.
Has the lowercase begin
any meaning in a Perl case sensitive programming context?
#!/usr/bin/perl
use v5.20;
use strict;
use warnings;
use feature qw(signatures);
no warnings 'once';
no warnings 'experimental';
no warnings 'experimental::signatures';
&if_exotic(1000);
&if_normal(1000);
# --------------------------------------
# Bad variant
# --------------------------------------
sub if_exotic($want) {
if ($want>0) {
print "BAD: ..running $want operations\n";
} begin {
print "BAD: ..nothing to do\n";
return;
}
print "Do something $want times\n";
}
# --------------------------------------
# Good variant
# --------------------------------------
sub if_normal($want) {
if ($want>0) {
print "GOOD: ..running $want operations\n";
} else {
print "GOOD: ..nothing to do\n";
return;
}
print "Do something $want times\n";
}
/usr/bin/perl "test-if-begin"
BAD: ..running 1000 operations
BAD: ..nothing to do
GOOD: ..running 1000 operations
Do something 1000 times