forked from szabgab/perlmaven.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscalar-found-where-operator-expected.tt
171 lines (121 loc) · 3.78 KB
/
scalar-found-where-operator-expected.tt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
=title Scalar found where operator expected
=timestamp 2013-02-15T01:5:06
=indexes syntax error, scalar found, operator expected
=status show
=books beginner_book
=author szabgab
=index 1
=archive 1
=feed 1
=comments 1
=social 1
=abstract start
This is a really common error message I see. One, that seems to be a bit difficult to understand.
The thing is, people think about <b>numerical operators</b> and <b>string operators</b>,
but they don't think about the comma <hl>,</hl> as an operator. For them the terminology of
the error message is a bit confusing.
Let's see a couple of examples:
=abstract end
<h2>Missing comma</h2>
The code looks like this:
<code lang="perl">
use strict;
use warnings;
print 42 "\n";
my $name = "Foo";
</code>
and the error message is
<code>
String found where operator expected at ex.pl line 4, near "42 "\n""
(Missing operator before "\n"?)
syntax error at ex.pl line 4, near "42 "\n""
Execution of ex.pl aborted due to compilation errors.
</code>
It clearly states the location of the problem, but as I see many people will rush
back to their editor trying to fix the issue even before reading the error message.
They make a change hoping that will fix the problem and then they get
another error message.
In this case the problem was that we have forgotten to add a comma <hl>,</hl> after the number 42.
The correct line would be <hl>print 42, "\n";</hl>.
<h2>String found where operator expected</h2>
In this code we have left out a concatenation operator <hl>.</hl>, and we got the same error message:
<code lang="perl">
use strict;
use warnings;
my $name = "Foo" "Bar";
</code>
<code>
String found where operator expected at ex.pl line 4, near ""Foo" "Bar""
(Missing operator before "Bar"?)
syntax error at ex.pl line 54, near ""Foo" "Bar""
Execution of ex.pl aborted due to compilation errors.
</code>
The intended code looks like this: <hl>my $name = "Foo" . "Bar";</hl>.
<h2>Number found where operator expected</h2>
<code lang="perl">
use strict;
use warnings;
my $x = 23;
my $z = $x 19;
</code>
Generates this error message:
<code>
Number found where operator expected at ex.pl line 5, near "$x 19"
(Missing operator before 19?)
syntax error at ex.pl line 5, near "$x 19"
Execution of ex.pl aborted due to compilation errors.
</code>
This code is probably missing an addition <hl>+</hl>, or multiplication <hl>*</hl> operator,
though that could be a repetition operator <hl>x</hl> as well.
<h2>Syntax error while comma is missing</h2>
A missing comma is not always recognized as a missing operator.
For example this code:
<code lang="perl">
use strict;
use warnings;
my %h = (
foo => 23
bar => 19
);
</code>
generates the following error message: <b>syntax error at ... line ..., near "bar"</b>
without any further details.
Adding a comma after the number 23 will fix the code:
<code lang="perl">
my %h = (
foo => 23,
bar => 19
);
</code>
I even prefer to add a comma after every pair in a hash (so in this case, after the number 19 too):
<code lang="perl">
my %h = (
foo => 23,
bar => 19,
);
</code>
This habit helps me to avoid this kind of syntax error most of the cases.
<h2>Scalar found where operator expected at</h2>
<code lang="perl">
use strict;
use warnings;
my $x = 23;
my $y = 19;
my $z = $x $y;
</code>
<code>
Scalar found where operator expected at ... line 7, near "$x $y"
(Missing operator before $y?)
syntax error at ... line 7, near "$x $y"
Execution of ... aborted due to compilation errors.
</code>
Again, there can be a numerical or a string operator between $x and $y.
<h2>Array found where operator expected</h2>
<code lang="perl">
use strict;
use warnings;
my @x = (23);
my $z = 3 @x;
</code>
<h2>What other cases do you encounter often?</h2>
Do you have other interesting cases where we get this type of syntax error?