Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Grokking Compile Error Messages

by QM (Vicar)
on Jul 21, 2004 at 15:35 UTC ( #376286=perlmeditation: print w/ replies, xml ) Need Help??

I came across a compile error today (OK, actually, I generated it, so who else was going to find it?) that took me some time to fix. I thought this would be a good place to gather similar examples of awkward or dense compile errors from everyone here.

Here's the error report (sorry for the wrapping):

C:\Perl\perl\BOD>perl -c bod_mask.pl "my" variable $rsp masks earlier declaration in same scope at bod_mask +.pl line 99. "my" variable $rsp_line masks earlier declaration in same scope at bod +_mask.pl line 99. "my" variable @bits masks earlier declaration in same scope at bod_mas +k.pl line 101. "my" variable $rsp masks earlier declaration in same scope at bod_mask +.pl line 101. "my" variable @bits masks earlier declaration in same scope at bod_mas +k.pl line 103. "my" variable $bit_index masks earlier declaration in same scope at bo +d_mask.pl line 103. "my" variable $time masks earlier declaration in same scope at bod_mas +k.pl line 105. "my" variable $space masks earlier declaration in same scope at bod_ma +sk.pl line 105. "my" variable @bits masks earlier declaration in same scope at bod_mas +k.pl line 105. syntax error at bod_mask.pl line 85, near ") (" Global symbol "$time" requires explicit package name at bod_mask.pl li +ne 85. Global symbol "$space" requires explicit package name at bod_mask.pl l +ine 85. Global symbol "$bits" requires explicit package name at bod_mask.pl li +ne 85. Global symbol "$bit_string" requires explicit package name at bod_mask +.pl line 86. syntax error at bod_mask.pl line 91, near ") {" syntax error at bod_mask.pl line 96, near "elsif" syntax error at bod_mask.pl line 106, near "}" bod_mask.pl had compilation errors.
Being trained from an early age on various "old school" compilers (MS Pascal version 1, for instance), generally one should avoid even looking at errors beyond the 1st or 2nd message, as they are usually caused by the earlier ones.

Being short on coffee and contact solution, I stared at this for quite a while. Searching my ~100 line script, there was only one my declaration for each of these variables! [Calm down -- Perl isn't picking on you. Perl is doing the best it can, given the shoddy input you gave it. You must be doing something wrong -- think.]

The scope hint started to sink in, so I went looking for missing }s and )s, as this is one of my frequent failings. I found a missing ) on an if conditional, and tried again. Now there were more my errors!

Calming down somewhat (no, drugs were not involved, but thanks for asking), I decided to isolate sections of code to narrow down the search for the offending line(s). I prematurely ended a few blocks with }s, and deftly placed an __END__ to isolate the problem section:

C:\Perl\perl\BOD>perl -c bod_mask.pl bod_mask.pl syntax OK
OK, I've narrowed it down to a few lines of code. Careful checking of all {} and () pairs (thank you for small miracles and smart editors), I found another lopsided if conditional. Wala! Problem solved!

But wait...Why did perl report all of those my errors, and not something more appropriate to missing )s?

It did, near the end:

syntax error at bod_mask.pl line 91, near ") {"
Moral: If the first error message doesn't help you find the cause, check through more of them. After all, Perl doesn't stop at the first error message because that may not be the cause of the error.

Feel free to contribute your own D'oh moments from compiler message head scratching sessions.

Brought to you by your local chapter of the Perl Error Message Society.

-QM
--
Quantum Mechanics: The dreams stuff is made of

Comment on Grokking Compile Error Messages
Select or Download Code
Re: Grokking Compile Error Messages
by hardburn (Abbot) on Jul 21, 2004 at 15:54 UTC

    so I went looking for missing }s and )s, as this is one of my frequent failings

    My way of solving this is to write out:

    if( condition ) { }

    And then go back and fill in the block. I almost never miss a curly. I tend to be lazier about doing the same with parens, since those are usually much shorter (and likewise tend to get bitten by unbalnced parens more often).

    ----
    send money to your kernel via the boot loader.. This and more wisdom available from Markov Hardburn.

      My way of solving this is to write out:
      if( condition ) { }
      Yes, that's helpful. However, I often have more complicated conditions, such as:
      if ( ( $flag1 or $flag2 ) and ( length( $string ) )
      [Note the missing ')']

      and that was the case this time as well.

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of

        if ( ( $flag1 or $flag2 ) and ( length( $string ) )

        This is where lining up your delimiters makes a lot of sense. Had you lined up the interior parens, giving

        if ( ( $flag1 or $flag2 ) and ( length( $string ) )
        the unbalanced leading paren would be a bit more obvious; in fact,
        if ( ( $flag1 or $flag2 ) and ( length( $string ) )
        might be even better: the if's leading paren isn't "masked" by the and.

        In this case, though, I'd be sorely tempted to break the if's condition out into a small sub with a meaningful name. I think

        if ( &condition( $flag1, $flag2, $string ) )
        is plenty readable, and gives you less punctuation to worry about.

        --
        F o x t r o t U n i f o r m
        Found a typo in this node? /msg me
        % man 3 strfry

        That can still be helped by better formatting rules. The way I write complex conditionals that would be too long to fit on one line is to treat the parens like curlies and line them up the same way, indenting the content.

        See what your mistake would look like then:

        if( ( $flag1 or $flag2 ) and ( length( $string ) ) { # ... }

        It still isn't perfectly obvious, but it's much easier to see now.

        It also helps to have an editor that will match brackets/parens/curlies for you; go through your delimiters one by one and check that there is a matching one where you expect it.

        Makeshifts last the longest.

Re: Grokking Compile Error Messages
by TimToady (Parson) on Jul 21, 2004 at 15:55 UTC
    Interestingly, this is one of those cases where turning on warnings obscured the real error. Maybe it would have helped if Perl printed warnings in yellow and errors in red. (Or otherwise distinguished fatal errors from warnings...)

      As soon as you do colored error messages, you'll get complaints from people on *nix systems that haven't been updated in 15 years about the weird output.

      Perhaps make it an option when you compile perl?

      ----
      send money to your kernel via the boot loader.. This and more wisdom available from Markov Hardburn.

        Perhaps make it an option when you compile perl?
        No, because on *nix, the sysadmin then has the unenviable position of annoying half of the people. And if you've ever been the sysadmin for a thousand users, someone will complain that the system wide time is off by 0.03 nanoseconds, never mind that Perl is giving them wonky characters in the error messages.

        Make it an environment option, or a command line option. And make it off by default.

        -QM
        --
        Quantum Mechanics: The dreams stuff is made of

      Go on, write a module that gives you colored error messages by setting $SIG{__DIE__} and $SIG{__WARN__}. Just don't make it default.

        You're all missing the point. My comment had almost nothing to do with a desire for colored error messages, and almost everything to do with designing a language compiler that won't confuse people by default, however that is to be accomplished. The basic underlying problem is that the messages are prioritized in a deceptive manner. An optional module does nothing to fix that.
Re: Grokking Compile Error Messages (details)
by tye (Cardinal) on Jul 21, 2004 at 16:13 UTC

    I'd like to see an example of the error such that Perl complains about multiple "my" declarations when there was only one.

    - tye        

      This code:
      #!/your/perl/here use strict; use warnings; if (( 1) # missing closing paren { my $x; ($x) = 'abc' =~ /(b)/; $x++; }
      generates these errors:
      C:\Perl\perl>perl -c syntax_error2.pl "my" variable $x masks earlier declaration in same scope at syntax_err +or2.pl line 9. "my" variable $x masks earlier declaration in same scope at syntax_err +or2.pl line 10. syntax error at syntax_error2.pl line 7, near ") # missing closing par +en {" syntax error at syntax_error2.pl line 11, near "}" syntax_error2.pl had compilation errors.
      In the real code, several variables (which had been declared outside of the if block) were referenced, and were reported as "masking".

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of

Re: Grokking Compile Error Messages
by BrowserUk (Pope) on Jul 21, 2004 at 16:34 UTC

    When you get a slew of errors and warnings all mixed together, you can reduce the clutter by disabling the warnings on the command line using -X option:

    P:\test>type junk.pl #! perl -slw use strict; my( $a, @b, %c ); if 1 ) { my( $a, @b, %c ); } P:\test>perl -c junk.pl "my" variable $a masks earlier declaration in same scope at junk.pl li +ne 8. "my" variable @b masks earlier declaration in same scope at junk.pl li +ne 8. "my" variable %c masks earlier declaration in same scope at junk.pl li +ne 8. syntax error at junk.pl line 7, near "if 1" junk.pl had compilation errors. P:\test>perl -Xc junk.pl syntax error at junk.pl line 7, near "if 1" junk.pl had compilation errors.

    This even seems to disable use warnings which is nice.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon
Re: Grokking Compile Error Messages
by stvn (Monsignor) on Jul 21, 2004 at 17:51 UTC

    Thats why I like my IDE (Apple's Project Builder), it *bonks* at me whenever I add to many or two few parens/brackets (open or close).

    -stvn
      You must have very forgiving neighbors. I would tend to bonk you every time your IDE bonked you. :p

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of

        You must have very forgiving neighbors. I would tend to bonk you every time your IDE bonked you.

        With all that bonking going on "friendly" might be a better term than "forgiving" :)

        -In Australia at least "bonking" is a euphemism for... another human activity *grin*

        --
        Do not seek to follow in the footsteps of the wise. Seek what they sought. -Basho

Re: Grokking Compile Error Messages
by QM (Vicar) on Jul 22, 2004 at 14:15 UTC
    I normally refrain from replying to my own posts.

    However, no one seems to have added their own anecdotes:

    I thought this would be a good place to gather similar examples of awkward or dense compile errors from everyone here.
    Hear Ye! Hear Ye! Hear Ye!

    Calling all honest coders: Please regale us with your darkest compiler error conundrums, stricture miseries, and "WTF?" moments!

    After all, the world loves a good story :)

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://376286]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (15)
As of 2014-09-19 12:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (137 votes), past polls