In the last few days I've tracked down some very odd bugs, mostly resulting from uncaught Perl syntax errors. Here are two I've recently seen.

I'm wondering what others we've seen. Perhaps a list we could refer to might save a lot of time consuming head scratching.

P.S. I tried to super search on this topic, but wasn't able to find a similar thread. Perhaps I just have weak search foo (I'm certain this topic has come up before), so if anyone can have better luck than I, perhaps those nodes could also be collected together and posted as a list so we can have a single place to look?

Replies are listed 'Best First'.
Re: Wierd bugs I would have never expected
by moritz (Cardinal) on Dec 16, 2010 at 12:52 UTC
    I somtimes get weird syntax errors from code that uses callbacks:
    get '/foo/bar' => sub { # 30 lines of sub here } # more code here

    The reason is that a named sub can end with just a curly bracket (and no semicolon), but the line from the callback needs to be finished with a semicolon. When I forget it, I get synatx errors somewhere below in the following code.

    The same happens with eval BLOCK.

    But the weirdest errors are not perl errors, but something that can happen in every programming language: accidentally running a different copy of the code than what you just modified. Either because it's in a different directory (or, even harder to find) because you're in an ssh session on one xterm, and on the local computer with another xterm. Since I have near-identical copies of my home directories on multiple machines, that can be a real pain to find out.

Re: Wierd bugs I would have never expected
by jdporter (Paladin) on Dec 16, 2010 at 14:24 UTC
    A module had the line return 1 outside of a sub.

    It's pretty normal for a module to have return 1 outside of a sub. Are you sure that's the cause of the problem you saw?

    What is the sound of Windows? Is it not the sound of a wall upon which people have smashed their heads... all the way through?

      At least as near as I can possibly see. It wouldn't be the first time I thought A was the issue when in fact B was. But I'm fairly certain. I narrowed it down by the old comment out code until nothing is left but the bare minimum to recreate the bug. The sample is pretty slim.

      There are in fact two possible ways to make the bug go away. (a) add a printf within the sub that wants to use the $SOME_CLASS or (b) comment out the return 1. Plain old 1; ( no return) at the end of the file works fine. It is only when the word return is added. I'm not inclined to blame the bug on failing to have a printf in the sub since that doesn't seem to me to be a reasonable requirement of proper use of package my variables.

      Here is the code that shows the problem. The test script:

      use strict; use warnings; use Monks::Foo::ReturnInModule; weAreOffToSeeTheWizard();

      The module Monks/Foo/ReturnInModule.pm:

        I can't replicate the issue with v5.10.x and v5.12.2 (while I can with an old 5.8.4). So, maybe it was a bug that's been fixed in the meantime... (?)

Re: Wierd bugs I would have never expected
by Anonyrnous Monk (Hermit) on Dec 16, 2010 at 13:31 UTC
Re: Wierd bugs I would have never expected
by Anonymous Monk on Dec 17, 2010 at 05:48 UTC
    The cause? a stray comma: print $fh, $sData instead of print $fh $sData.
    I catch myself making the same mistake, and I've begun to habitually use the form print {$filehandle} $payload; to notify both me and Perl what I really mean. I'm amazed at how quickly "print" followed by curly braces feels like a new sigil; and it really decreases the amount of time I spend examining such statements for intent, not to mention examining them for errors.
Re: Wierd bugs I would have never expected
by ELISHEVA (Prior) on Jan 03, 2011 at 14:49 UTC

    My latest wierd bug: calling rmdir created a file, even as it removed a directory.

    Not really of course. What actually happened is that a symbolic link that once counted as a directory got reclassified as a non-directory when its target (a directory) got deleted.

    I had constructed a list of files I wanted to delete. The list contained only directory "A" and its sibling non-directories. When I reran the query that generated the original deletion list instead of finding no files, two unexpected files "L" and "L1" suddenly appeared.

    It just so happened that "L" and "L1" were linked through a long chain of links to "A". As long as "A" existed they were classified as directories and did not appear on my generated list of things to delete. As soon as "A' was deleted, these links lost their directory classification and showed up as "non-directories" on my list of things to delete. No files created, just a simple reclassification.

    Beware mixing File::Find, file type checks, symbolic links, and file/dir removal.

Re: Wierd bugs I would have never expected
by hexcoder (Curate) on Dec 23, 2010 at 22:13 UTC
    while (1) { # use a block for structuring things { print "do something, then leave the loop\n"; last; } }
    Of course this coooompletely different to
    while (1) { # use a block for structuring things (a bit unconventionally) if (1) { print "do something, then leave the loop\n"; last; } }
    Do you know what happens here? :-)) It has cost me some time to find out...
    Happy xmas to all.