CGI syntax error not behaving as I expected but...

I wrote a perl script to allow me to look at portions of the log that is
kept for people searching. It was written some time ago and seemed to
work perfectly. It was not until last week that I noticed that after
12pm the time would jump back an hour and stay that way until 12am. I
suspected a bug but doubled check incase of some weird virus or something.

Of course the error is in the elsif statement it should be ==12 not =12.
So of course it works in the am the pm messes up.

My question is this: I thought a syntax error of this type would not run
and that I would get an error with no output? Or is this type of error
considered a warning?

I am fairly inexperienced in perl and usually have to write stuff as "we
need this up tommorrow. Figure it out quick" kinda of situation which is
not a good way to actually learn and understand. I also do most of perl
work in the morning and it did not occur to change the clock and test
different hours. Lesson learned.

Advertisements

mmosher <> wrote:
> Of course it was a bug:
>
> if (substr($date_time_raw[$loop],8,2)<12) {##do something}
> elsif (substr($date_time_raw[$loop],8,2)=12) {##do something}
>
> else
> {##do something}
>
> My question is this: I thought a syntax error of this type would not run
> and that I would get an error with no output? Or is this type of error
> considered a warning?

Using substr as an lvalue like this is perfectly valid Perl. Read
perldoc -f substr again.

perl -le'my $x = "abc"; substr($x, 1, 1) = "XXX"; print $x'

Ben

--
If you put all the prophets, | You'd have so much more reason
Mystics and saints | Than ever was born
In one room together, | Out of all of the conflicts of time. |----------------+---------------| The Levellers, 'Believers'

Advertisements

On Tue, 3 Feb 2004, mmosher wrote:
> Of course it was a bug:
>
> if (substr($date_time_raw[$loop],8,2)<12) {##do something}
> elsif (substr($date_time_raw[$loop],8,2)=12) {##do something}
>
> else
> {##do something}
>
> Of course the error is in the elsif statement it should be ==12 not =12.
> So of course it works in the am the pm messes up.
>
> My question is this: I thought a syntax error of this type would not run
> and that I would get an error with no output? Or is this type of error
> considered a warning?
>

That's not a syntax error. Assigning a value to a substring is a
perfectly legal operation. It's just not what you wanted to do. That
line says "find the 2 character substring from the variable
$date_time_raw[$loop] starting at position 8, and replace it with the
string "12". There's no syntax error there at all.

Ben Morrow <> wrote:
>
> mmosher <> wrote:
> > elsif (substr($date_time_raw[$loop],8,2)=12) {##do something}
> >
> > My question is this: I thought a syntax error of this type would not run
> > and that I would get an error with no output? Or is this type of error
> > considered a warning?
>
> Using substr as an lvalue like this is perfectly valid Perl. Read
> perldoc -f substr again.

This is precisely why you might see some (particularly C code) written
like:
if (1 == someVar) {...}
because the typo
if (1 = someVar) {...}
_would_ trigger an error.

Thank you both for the explaination. Should have relized with was my
logic error and not syntax. My thinking was that since it was in an if
statement I could not do an assignment. Everything makes sense now. I'll
check that document.

Yes I can be taught.

mmosher wrote:
> ... doing exactly what its supposed to do.
>
> I wrote a perl script to allow me to look at portions of the log that
> is kept for people searching. It was written some time ago and seemed
> to work perfectly. It was not until last week that I noticed that
> after 12pm the time would jump back an hour and stay that way until
> 12am. I suspected a bug but doubled check incase of some weird virus
> or something.
>
> Of course it was a bug:
>
> if (substr($date_time_raw[$loop],8,2)<12) {##do something}
> elsif (substr($date_time_raw[$loop],8,2)=12) {##do something}
>
> else
> {##do something}
>
> Of course the error is in the elsif statement it should be ==12 not
> =12. So of course it works in the am the pm messes up.
>
> My question is this: I thought a syntax error of this type would not
> run and that I would get an error with no output? Or is this type of
> error considered a warning?
>
> I am fairly inexperienced in perl and usually have to write stuff as
> "we need this up tommorrow. Figure it out quick" kinda of situation
> which is not a good way to actually learn and understand. I also do
> most of perl work in the morning and it did not occur to change the
> clock and test different hours. Lesson learned.
>
> Thank you.
>

mmosher wrote:
> Thank you both for the explaination. Should have relized with was my
> logic error and not syntax. My thinking was that since it was in an if
> statement I could not do an assignment. Everything makes sense now. I'll
> check that document.
>
> Yes I can be taught.

Thomas Kratz wrote:
> mmosher wrote:
>
>> Thank you both for the explaination. Should have relized with was my
>> logic error and not syntax. My thinking was that since it was in an
>> if statement I could not do an assignment. Everything makes sense
>> now. I'll check that document.
>>
>> Yes I can be taught.
>
>
> Can you be taught to stop top posting?
>
> Thomas
>

Sorry about that. Even though I don't post much I do know about that and
for some reason I didn't post correctly.

Jim Gibson <> wrote:
>
> The same compiler laxity is present in C and C++, and this is a common
> error there, too. I have seen programmers turn the conditional around,
> putting the constant first:
>
> if( 12 == substr($date_time_raw[$loop] ) { ## do something }
>
> Then, if they forget the second equal sign, the compiler terminates
> with a syntax error:
>
> "Can't modify constant item in scalar assignment at myprogram.pl line
> 2, near ") ) "
>
> I don't do this because I don't like the readability of the result, but
> it does work.

I, OTOH, often do do this, because I much prefer list operators
without parentheses:

if (12 == substr $data_time_raw[$loop], 8, 2) { }

TMTOWTDI, as usual

Ben

--
It will be seen that the Erwhonians are a meek and long-suffering people,
easily led by the nose, and quick to offer up common sense at the shrine of
logic, when a philosopher convinces them that their institutions are not based
on the strictest morality. [Samuel Butler, paraphrased]

JG> As pointed out by others, this is not a syntax error. However, if you
JG> had put "use warnings" at the top of your program, perl would have
JG> flagged the assignment in the if conditional as a warning:

On Tue, 03 Feb 2004 18:10:35 +0100, Thomas Kratz
<> wrote:
>> Thank you both for the explaination. Should have relized with was my
>> logic error and not syntax. My thinking was that since it was in an if
>> statement I could not do an assignment. Everything makes sense now. I'll
>> check that document.
>>
>> Yes I can be taught.
>
>Can you be taught to stop top posting?

Also, it is customary that people post here (OT) CGI questions wrongly
assuming that CGI==Perl, without even specifying in the subject line
that their post is about CGI. This, along with the actual nature and
tone of those questions, tends to cause irritation in many group
regulars.

Now you have the opposite situation, where you had a true Perl
question that you presented like a CGI one: note that many valuable
contributors to this ng may have kept away from this thread because of
this and the above mentioned circumstance!

I suggest you try to choose more pertinent subjects for the future...

Michele
--
you'll see that it shouldn't be so. AND, the writting as usuall is
fantastic incompetent. To illustrate, i quote:
- Xah Lee trolling on clpmisc,
"perl bug File::Basename and Perl's nature"

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!