in reply to Aborting a module

You can return 1; from a module and the rest won't be processed. I suspect that would also work from a BEGIN block but I'll let you test that.

- tye        

Replies are listed 'Best First'.
Re^2: Aborting a module (return 1)
by FunkyMonk (Bishop) on Oct 23, 2007 at 20:17 UTC
    I thought that BEGIN { } was just magic subroutine, so a return just returns from the BEGIN block. My tests seem to confirm this:
    sub BEGIN { print "hello\n"; return 1; } print "bye\n"

    prints (wait for it...)

    hello bye

      Yes, precisely. I should have realized that. Thanks.

      - tye        

Re^2: Aborting a module (return 1)
by andreas1234567 (Vicar) on Oct 23, 2007 at 20:05 UTC
    Like this? It seems to continue past the BEGIN block.
    $ perl -wl use strict; BEGIN { return 1; } print "foo"; __END__ foo
    --
    Andreas

      Rereading the root node, the BEGIN was an example of a complicated way of using the module rather than code within the module. So the following should work fine:

      package My::Module; if( ... ) { # This module is useless in this case: return 1; } ... 1;

      Another possiblity is:

      package My::Module; if( ... ) { require My::Module::Internal; } 1;

      - tye        

        Yeah, the module in the middle method is the only clean way I can see. The if statement works but I also have to "use if" all the modules.

        Basically we have a module that we include everywhere to set up our environment, but we recently ran into a problem where we called someone else's Perl script from the module if a value wasn't set, which loaded our module, which called itself, which loaded our module.... etc etc etc *disaster*.

        So now we are trying to make it so that only our code calls it, even though it is set in PERL5OPT

                        - Ant
                        - Some of my best work - (1 2 3)

      Confirmed, it doesn't work from within BEGIN.

      >type M.pm package M; BEGIN { return 1; } print "Point A\n"; return 1; print "Point B\n"; 1; >perl -e "use M;" Point A
Re^2: Aborting a module (return 1)
by suaveant (Parson) on Oct 24, 2007 at 14:34 UTC
    Yeah... it's annoying... you can return from the BEGIN block but it doesn't stop anything except the processing of the rest of that BEGIN. Ah well.

                    - Ant
                    - Some of my best work - (1 2 3)