Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Running modulino inside BEGIN

by LanX (Saint)
on Apr 14, 2024 at 17:22 UTC ( [id://11158864]=perlquestion: print w/replies, xml ) Need Help??

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

Hi

while developing a module I like to run (F5) the testsuite automatically. I'm using a moulino approach which works considerably well

#BEGIN { # TODO unless ( caller() ) { # Modulino for development exec q(cd ../..; prove ./t/); #run proove from dist root } #}

It occurred to me that I have to enclose this inside a BEGIN block to avoid the overhead and side-effects of loding all modules with use (this happens in the BEGIN phase) before the unless is executed at run time.

This doesn't work, since caller() IS defined inside BEGIN

BEGIN { warn "Caller Begin: ",defined caller(0) } warn "Caller Normal: ",defined caller();

Caller Begin: 1 at /home/lanx/... Caller Normal: at /home/lanx/...

EDIT Solution

while writing this I realized that BEGIN is called 2 levels down, hence caller(2) seems to work

BEGIN { # TODO unless ( caller(2) ) { # Modulino for development exec q(cd ../..; prove ./t/); #run proove from dist root } }

Is this reliable?

(I think it is, but I already wrote this post, before finding the solution and the monastery is not overwhelmed with writeups :)

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

Replies are listed 'Best First'.
Re: Running modulino inside BEGIN
by hv (Prior) on Apr 14, 2024 at 19:51 UTC

    There was a recent(ish) change in this area that caused some fallout - I think the one referred to in #15109, which should be visible as a change between 5.34.0 and 5.36.0. So I recommend checking if you get the same behaviour across those two versions.

      Oh, thanks a lot.

      I'll avoid BEGIN till it's settled.

      Or maybe I'll write a my module for this.

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

Re: Running modulino inside BEGIN
by NERDVANA (Curate) on Apr 14, 2024 at 19:59 UTC
    Are you not able to alter the behavior of F5 to run an arbitrary perl script?

    This seems like a situation I'm having with a co-worker who insists on starting every .pm with

    use Cwd (); use lib Cwd::abs_path(__FILE__) =~ s{/@{[__PACKAGE__ =~ s{::}{/}gr] +}\.pm$}{}r; use lib Cwd::abs_path(__FILE__) =~ s{lib/@{[__PACKAGE__ =~ s{::}{/}gr] +}\.pm$}{Schema/lib}r;
    so that he can run perl -c on the file and have it find all its modules. He seems resistant to just have the compile-check run a more advanced script that initializes the library path for him.
      > you not able to alter the behavior of F5 to run an arbitrary

      Sure, I can either redefine F5 or setup another key for testing, or even set a config-line at the top of the module redefining F5 only for this file.

      I prefer a Perl solution which is editor agnostic.

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

Re: Running modulino inside BEGIN
by ikegami (Patriarch) on Apr 15, 2024 at 13:28 UTC

    If the level is inconsistent, you could check if there's any level with another package.

    package Foo; use strict; use warnings; use feature qw( say ); sub is_modulino { for ( my $i = 1; ; ++$i ) { my $caller = caller( $i ); return 1 if !defined( $caller ); return 0 if $caller ne __PACKAGE__; } } BEGIN { say is_modulino() ? 1 : 0; } 1;
    $ perl -I . -e'use A;' 0 $ perl A.pm 1

    A shorter version could be obtained by unrolling the first two passes of the loop.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11158864]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (9)
As of 2024-05-20 15:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found