Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Perl syntax checking without `perl -c`

by LanX (Saint)
on Dec 03, 2020 at 02:15 UTC ( [id://11124561]=note: print w/replies, xml ) Need Help??


in reply to Perl syntax checking without `perl -c`

Hi Ken,

> like 'perl -c ...', that will just check the syntax without executing any part of the code?

the problem is that imported functions can change the syntax.

Just think of something like sub foo (&) {} , which will only compile if it finds foo {...} later. That's the main reason why Perl is so hard to parse statically.

> The use statements will be my biggest problem; although, others may crop up here and there.

> "Can't locate SomeMod.pm in @INC ..."

OK let's suppose the exported functions don't change the syntax.

What you could do is pretending that "SomeMod.pm" was already loaded, by setting %INC in a BEGIN block.* No need to find a module which was already "loaded".

But these are kind of hairy workarounds, do you really think it's worth going down that road?

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

*) I seem to remember that perlrun allows you to inject a BEGIN without changing the actual code, just the way you start it.

Replies are listed 'Best First'.
Re^2: Perl syntax checking without `perl -c`
by kcott (Archbishop) on Dec 03, 2020 at 02:37 UTC

    G'day Rolf,

    "the problem is that imported functions can change the syntax"

    You make a very good point; I hadn't considered that. I've only downloaded individual files (e.g. Whatever.pm) and these are all integrated into a much bigger framework; there will, in a number of places, be modules that change syntax.

    I really just wanted to get on with some work while the SysAdmin folks dealt with the disaster recovery. Setting up the environment — which not only involves code but also databases, web servers, etc. — would likely take substantially longer than the recovery (and, as I indicated above, depending on services affected, may not even be possible at this time).

    "But these are kind of hairy workarounds, do you really think it's worth going down that road?"

    No, I don't think so. :-)

    For now, I'll just rely on my editor's syntax highlighting and my own experience. In a day or two, I can upload all my changes and test in the full environment.

    Thanks for your feedback.

    — Ken

Re^2: Perl syntax checking without `perl -c`
by choroba (Cardinal) on Dec 03, 2020 at 10:43 UTC
    Another workaround I sometimes use at work is to create a fakelib/ directory with contents created like
    echo 1 > SomeMod.pm

    Then include fakelib into your PERL5LIB (or run perl -I /path/to/fakelib).

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      Seconded; I've used basically the same thing letting flycheck run on my desktop working copy at $work so that it's perl -c check behaves even though I don't have the same perl environment available as the production machines where the code actually runs.

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

        The problem I see with this filesystem approach is maintenance.

        What if a missing module was installed in the meantime?

        A module "FakeInc" called via something like perl -MFakeInc -c could check if the module is available before installing a facade.

        ~~~

        Thinking about it, having a code hook at the last position of @INC could also handle all missing modules...

        from require : You can also insert hooks into the import facility by putting Perl code directly into the @INC array. There are three forms of hooks: subroutine references, array references, and blessed objects.

        update

        oops ... I didn't realized there was already a demo: Re: Perl syntax checking without `perl -c`

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

      Yeah, that's another good approach.

      I think it depends how many modules one needs to fake and often one needs them.

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11124561]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-04-26 06:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found