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

Discussion in 'Perl Misc' started by mmosher, Feb 3, 2004.

  1. mmosher

    mmosher Guest

    .... 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, Feb 3, 2004
    #1
    1. Advertising

  2. mmosher

    Ben Morrow Guest

    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'
     
    Ben Morrow, Feb 3, 2004
    #2
    1. Advertising

  3. mmosher

    Paul Lalli Guest

    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.

    Paul Lalli
     
    Paul Lalli, Feb 3, 2004
    #3
  4. 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.


    --
    Glenn Jackman
    NCF Sysadmin
     
    Glenn Jackman, Feb 3, 2004
    #4
  5. mmosher

    mmosher Guest

    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, Feb 3, 2004
    #5
  6. mmosher

    Thomas Kratz Guest

    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


    --
    open STDIN,"<&DATA";$=+=14;$%=50;while($_=(seek( #J~.> a>n~>>e~.......>r.
    STDIN,$:*$=+$,+$%,0),getc)){/\./&&last;/\w| /&&( #.u.t.^..oP..r.>h>a~.e..
    print,$_=$~);/~/&&++$:;/\^/&&--$:;/>/&&++$,;/</ #.>s^~h<t< ..~. ...c.^..
    &&--$,;$:%=4;$,%=23;$~=$_;++$i==1?++$,:_;}__END__#....>>e>r^..>l^...>k^..
     
    Thomas Kratz, Feb 3, 2004
    #6
  7. mmosher

    mmosher Guest

    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.
     
    mmosher, Feb 3, 2004
    #7
  8. mmosher

    Ben Morrow Guest

    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]
     
    Ben Morrow, Feb 4, 2004
    #8
  9. mmosher

    Uri Guttman Guest

    >>>>> "JG" == Jim Gibson <> writes:

    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:

    JG> "Found = in conditional, should be == at myprogram.pl line 2."

    that warning is not worded correctly. look at these:

    perl -we 'if ( my $x = 3 ) {print "foo\n"}'
    Found = in conditional, should be == at -e line 1.
    foo

    perl -we 'if ( my $x = $y ) {print "foo\n"}'
    Name "main::y" used only once: possible typo at -e line 1.

    perl -we 'if ( $x = $y ) {print "foo\n"}'
    Name "main::x" used only once: possible typo at -e line 1.
    Name "main::y" used only once: possible typo at -e line 1.

    that warning only happens when you assign a constant in the
    conditional. i use if ( my $x = $y ) in plenty of places. $y is really
    most commonly a hash lookup like this:

    if ( my $foo = $self->{'foo'} ) {

    do something with $foo
    }

    this keeps $foo tightly scoped. it also eliminates the extra hash lookup
    that would be needed otherwise. the typical ways that is coded is:


    if ( $self->{'foo'} ) {

    do something with $self->{'foo'}
    }

    or

    my $foo = $self->{'foo'} ;
    if ( $foo ) {

    do something with $foo
    }

    JG> The same compiler laxity is present in C and C++, and this is a common
    JG> error there, too. I have seen programmers turn the conditional around,
    JG> putting the constant first:

    JG> if( 12 == substr($date_time_raw[$loop] ) { ## do something }

    and another point i should mention to the OP, substr deal with strings
    so the == should be eq. and if it was that, then the confusion over = vs
    == wouldn't happen.

    this constant on the left trick is old and the book code complete
    mentions it. since i don't fall into the =/== bugs (too) often, i don't
    worry about it.

    JG> I don't do this because I don't like the readability of the result, but
    JG> it does work.

    same here.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Feb 4, 2004
    #9
  10. 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"
     
    Michele Dondi, Feb 5, 2004
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ken Kafieh
    Replies:
    7
    Views:
    2,502
    Roedy Green
    May 19, 2004
  2. William Krick
    Replies:
    4
    Views:
    77,916
    rt19983
    Dec 18, 2008
  3. Skip Montanaro

    sys._getframe() not behaving as expected

    Skip Montanaro, Dec 1, 2003, in forum: Python
    Replies:
    0
    Views:
    368
    Skip Montanaro
    Dec 1, 2003
  4. Replies:
    1
    Views:
    351
    David
    Apr 16, 2007
  5. Steve Richter
    Replies:
    2
    Views:
    614
    Steve Richter
    May 29, 2007
Loading...

Share This Page