The quality of Perl questions on Stackoverflow have been rather depressing lately. When I saw “Why does C = 16?”, I thought it was par for the course. Here is a screenshot, in case the question itself disappears:
Notice, the first line of the script is:
int a, b, c;
@toolic rightfully objects to that line:
int a, b, c; is not Perl syntax. – toolic
Of course, you know that, in Perl, int a, b, c;
does not declare three integer variables called a, b, and c.
But, what does it do?
Given that the OP is running without strictures or warnings, this line will raise no red flags.
First, int is a Perl-builtin.
Returns the integer portion of EXPR. If EXPR is omitted, uses $_.
This line is evaluated in void context. When it is run, three expressions are evaluated:
int a
b
c
The bareword a is not the name of a defined subroutine, so it just evaluates to “a”. Now, “a” is not numeric, so int "a"
evaluates to zero.
Similarly, b evaluates to “b”, and c evaluates to “c”.
Therefore, int a, b, c;
is basically 0, "b", "c";
in void context.
Now, of course, if one added use strict;
before this line, as one really ought to, one would simply get:
$ perl -Mstrict -e 'int a, b, c'
Bareword "b" not allowed while "strict subs" in use at -e line 1.
Bareword "a" not allowed while "strict subs" in use at -e line 1.
Bareword "c" not allowed while "strict subs" in use at -e line 1.
Execution of -e aborted due to compilation errors.
Unless you are writing Perl poetry, use strict.
Now, explain the difference between:
$ perl -le '$x = int a, b, c; print $x'
and
$ perl -le '$x = (int a, b, c); print $x'