xavier8854 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

Seems to be a dumb question, but I found no solution. I'm ashamed that I have submitted to CPAN a module, with a typo

reeturn $self->someMethod()

Nothing catched this, obviously perl -cw myModule.pm didn't, but the tool Perl::Lint didn't either... Besides being less stupid, or more cautious, do you have an idea ?

Thanks,

Regards,

Xavier

Replies are listed 'Best First'.
Re: Catching typos
by haukex (Archbishop) on Jan 06, 2021 at 17:58 UTC

    I like the others' suggestions of Perl::Critic (which at least warns that "Subroutine "foo" does not end with "return"") and no indirect, the former of which I use for my modules. I just wanted to add another point that hasn't been mentioned yet: having a test suite with good code coverage (Devel::Cover) helps too; almost all of my CPAN modules have 100% code coverage. Though that's not a guarantee that everything is caught, it at least checks that each line of code is executed at least once, which would hopefully have helped in this particular situation.

Re: Catching typos
by hippo (Archbishop) on Jan 06, 2021 at 16:49 UTC
    Nothing catched this, obviously perl -cw myModule.pm didn't

    A quick test on the commandline shows that this is treated as indirect object notation which perlcritic should pick up on. Perhaps that's one way to go.

    Did your test suite not raise failures?


    🦛

      There is at least a cpan pragma indirect to disable that notation.

      Unfortunately it requires XS.

      Would be nice to have something alike in core.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

      I think you nailed it ...

      But I didn't expect this precedence

      d:\tmp\pm>perl -MO=Deparse -e "reeturn $self->someMethod()" $self->reeturn->someMethod; -e syntax OK d:\tmp\pm>

      Obfuscators would party now... ;-)

      wait ... look at this:

      d:\tmp\pm>perl -MO=Deparse -e "A B C D" 'B'->A('D'->C); -e syntax OK d:\tmp\pm>

      Forget the lockdowns and lets meet for 99 beers ... xD

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

Re: Catching typos
by GrandFather (Saint) on Jan 07, 2021 at 04:04 UTC

    The problem isn't finding this specific error again because the chance that it will happen and not be caught by you is now pretty small. The better question is "how do I catch silly error in my code before publishing it?" and the answer is: code coverage and tests.

    Devel::Cover is the go to for code coverage. Test is a starting point for writing a test framework, although the testing support for Perl is vast.

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
Re: Catching typos
by stevieb (Canon) on Jan 06, 2021 at 16:51 UTC

    hippo quickly came up with at least one possible reason. Any half decent editor or IDE would catch this typo and at least warn about it.

    I can't even think of an instance where that would fly, unless you've declared a reeturn() sub.

    In what context is this used in?

      > Any half decent editor or IDE would catch this typo and at least warn about it.

      Really? Most IDE depend on running perl -c for checks.

      Emacs syntax highlighting and indentation help me in similar cases , but that's not a warning. ..

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        My IDE highlights the word in yellow (warning), and hovering over it produces the following pop-up: Unable to find sub definition, declaration, constant definition or typeglob aliasing. It also throws a yellow dot at the line where the warning is located, so even if that line isn't in view on the screen, I can always see if there are any warnings/errors anywhere within the file at a glance. Everything happens live-time, so I can actually see it immediately after I'm done typing.

        Here's an image of what I'm speaking of. If I point at the word, the notice pops up.

Re: Catching typos
by Bod (Parson) on Jan 06, 2021 at 22:21 UTC