Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Proper handling of shebang line while performing local testing?

by nysus (Vicar)
on Sep 05, 2018 at 05:13 UTC ( #1221727=perlquestion: print w/replies, xml ) Need Help??

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

I have a cpan module the supplies a shell script with. I originally set the shebang line to #!/usr/bin/env perl until someone told me that cpan modules should run with #!perl or #!/usr/bin/perl.

I made the change but the problem I'm having now is that my tests are now failing because I do not have the required modules installed in my system perl. I'm using distzilla to release my distribution. What is the proper way to address this problem aside from installing all the modules on system perl?

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: Proper handling of shebang line while performing local testing?
by Corion (Pope) on Sep 05, 2018 at 06:56 UTC

    Maybe that someone was wrong.

    You should have the perl (or whatever program) in the hashbang line which is used by the kernel to run the subsequent code. For shell scripts that is /bin/sh (or whatever), and for Perl scripts, that should resolve to the appropriate Perl interpreter.

    Using the redirection through /usr/bin/env is en vogue with some people, but hardcoding /usr/bin/perl if that's not your Perl is just wrong.

        Maybe then you need to invoke your tests in a different way. I usually run single tests as

        perl -Mlib -w t/01-api.t

        ... and the whole test suite with

        make test

        or, if I want to run the author tests as well, with:

        make prove -bl xt/ t/

        Directly starting a test will get things confused because the kernel will invoke the Perl that is hardcoded in the first line instead of using the Perl that you (may) want.

Re: Proper handling of shebang line while performing local testing?
by dsheroh (Prior) on Sep 05, 2018 at 07:30 UTC
    You mentioned that you're using distzilla. This presents a potential solution: Write your code with #!/usr/bin/env/perl (so that it works with your local tests) and then use a distzilla plugin to rewrite the Perl files while they're being packaged for distribution, changing #!/usr/bin/env/perl to #!/usr/bin/perl (so that the distributed files will be CPAN-correct according to your "someone").

    To the best of my knowledge, such a distzilla plugin does not currently exist, but it's a simple enough task (run one regex substitution on the first line of each file) that writing one should be relatively straightforward.

      This should not be needed. When testing locally just use invocations like prove -lr t/ and perl script/foo.pl so that the shebangs are not used. There is another underlying issue here.
        Er, perl -Ilib script/foo.pl
Re: Proper handling of shebang line while performing local testing?
by Anonymous Monk on Sep 19, 2018 at 17:46 UTC
    Run your tests using the correct Perl. The normal invocation is prove -lr t/, but if you're using Dist::Zilla you can just do dzil test. Your post implies that your tests are running your included shell script though, which indicates they are executing it wrong. Your tests must use $^X as the perl executable so they run using the Perl being installed to.

      Thanks. I can't remember now but I think I was using dzil test and it still wasn't working (but this was before I had the `perl` command in there to try to get it to work on Windows). I will try with the $^X special variable. I didn't know that existed until now.

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest";
      $nysus = $PM . ' ' . $MCF;
      Click here if you love Perl Monks

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2019-07-22 12:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If you were the first to set foot on the Moon, what would be your epigram?






    Results (16 votes). Check out past polls.

    Notices?