http://qs1969.pair.com?node_id=11133675


in reply to How to syntax-check Catalyst code which throws 'Invalid CODE attributes' errors?

Itís not Moose, itís specifically Catalystís handling of the information. Probably your Catalyst code is either wonky or relies on the presence of the base packages to already be loaded in such a way that sub packages will fail because they are missing dependent information. Compare these twoĖ

perl -c -e 'sub taco :Chained("/") :PathPart("foo") : CaptureArgs(0) { +}' Invalid CODE attributes: Chained("/") : PathPart("foo") : CaptureArgs( +0) at -e line 1. BEGIN failed--compilation aborted at -e line 1.
perl -c -e 'use parent "Catalyst::Controller"; sub taco :Chained("/") +:PathPart("foo") : CaptureArgs(0) {}' -e syntax OK

Side-note about the -c flag. Itís not just a syntax check, it has to compile code and that means running some of it and there is no way to know what that might include without stepping through it. For exampleĖ

# emacs/vi UseMe.pm package UseMe; print STDERR "rm -rf /"; perl -c UseMe.pm UseMe.pm syntax OK perl -c -e 'use UseMe' rm -rf /-e syntax OK

Replies are listed 'Best First'.
Re^2: How to syntax-check Catalyst code which throws 'Invalid CODE attributes' errors?
by LittleJack (Beadle) on Jun 09, 2021 at 04:48 UTC

    That looks like exactly what I need, thank you. However I'm not quite understanding how I would use your inline example in my real-life situation, where I'm doing something like this:

    perl -I /some/path/perl5 -c Path/To/lib/Controller/Foo.pm

    How can I include your use parent code in that command?

      You can do that but I think youíre probably going to be happier testing the application itself instead of components; or testing the components from within the application. Controllers need to have a notion of the Catalyst context. Essentially, they donít even properly exist except as part of a request dispatch. A controller endpoint cannot naturally do anything outside of that.

      What youíre doing is akin to trying to check subs inside a script instead of the script itself. It can be done but itís weird given the, well, context of the code. Catalyst controllers arenít code that can be run alone, only within and by the app. Side-note: models and views differ here. Catalyst only adds configuration and access affordances for those.

      Catalyst testing is really extremely good and flexible. Only Mojo is better, easier. It doesnít require any (system, standalone, external) server or special environment or anything.

        I totally understand your point about not testing one file at a time but I'm in a tricky situation with source control which means I really need to to test things in isolation sometimes. I simply need to test for basic things like typos at one point in the lifecycle. So ... how would I do that?