in reply to Require a Modul that produced a error before

require won't do anything the second time around since the module has been marked as included the first time around (even though it didn't load completely).

You could clear the %INC entry on failure, but that could lead to very odd (i.e. hard to debug) errors.

eval { require Module }; print $@ ? "Error\n" : "Success\n"; # Error { local $, = ", "; local $\ = "\n"; print keys %INC; } eval { require Module }; print $@ ? "Error\n" : "Success\n"; # Success
# Module.pm for;

Replies are listed 'Best First'.
Re^2: Require a Modul that produced a error before
by Outaspace (Scribe) on Oct 12, 2006 at 22:58 UTC
    What did you ment by:
    "You could clear the %INC entry on failure, but that could lead to very odd (i.e. hard to debug) errors."
    I tested it and so far it worked.
    Is it safe to use it, or not ? Are there any other things that maybe can be reverted ? Would you use it ? Where can I find additional sources, that I can take a look at ?

    It would only be needed for development, but if it produced odd errors I cant use it.

    Thanks so far,

    Andre

      Sorry for the delayed reply.

      In the examples below, I used the following test harness:

      my $module = do { open(my $fh, '<', 'Module.pm') or die("Unable to read Module.pm: $!\n"); local $/; <$fh> }; eval $module or warn $@; $module =~ s/^for;$/#for;/m; eval $module or warn $@;
      • The most common problem would be (safely ignored) warnings.

        use strict; use warnings; package Module; sub one { } for; sub two { } 1;

        First require/eval output:

        [None]

        Second require/eval output:

        Subroutine one redefined at (eval 2) line 6.
      • Another problem is code running twice.

        use strict; use warnings; package Module; END { print("END1\n"); } for; END { print("END2\n"); } 1;

        First require/eval output:

        [None]

        Second require/eval output:

        END2 END1 END1
      • Same, but different.

        use strict; use warnings; package Module; BEGIN { print("BEGIN1\n"); } for; BEGIN { print("BEGIN2\n"); } 1;

        First require/eval output:

        BEGIN1

        Second require/eval output:

        BEGIN1 BEGIN2
      • Objects can be created and destroyed twice.

        use strict; use warnings; package Module; { package Obj; sub new { print "Creating\n"; bless({}, shift) } sub DESTROY { print "Destroying\n"; } } our $o; BEGIN { $o = Obj->new(); } for; 1;

        First require/eval output:

        Creating

        Second require/eval output:

        Subroutine new redefined at (eval 2) line 8. Subroutine DESTROY redefined at (eval 2) line 9. Creating Destroying Destroying

        Hopefully, nothing took the address of $o in between the two requires!

      That's all I can think of. It's not quite as bad as I thought, since I thought a function could capture a variable from the first attempt, and another function could capture the same variable from the second attempt. Still, the above can still create some weird problems.

        Thanks, since I only would use it to speed up the development process, I can use this approach. Also the Modules that are required are all OO and I try to keep those things out of it. The rest should be done by the Perl Carbage Collection.

        Andre