http://www.perlmonks.org?node_id=1228760


in reply to Error: Attempt to reload module.. while testing failing require

Note the pseudocode documented in require: If a module fails to compile (do returns undef), it will still get an %INC entry of undef, meaning that the entry still exists, but it indicates a failed load.

$ echo "die" > Whatever.pm $ perl -wMstrict -MData::Dumper -I. -e 'eval {require Whatever}; print + Dumper({%INC{"Whatever.pm"}})' $VAR1 = { 'Whatever.pm' => undef };

perldiag says about that error message: "Attempt to reload %s aborted. (F) You tried to load a file with use or require that failed to compile once already. Perl will not try to compile this file again unless you delete its entry from %INC. See require and %INC in perlvar." So deleteing the %INC entry is the way to go, if you want to structure your code this way.

By the way, is the source of Win32::Backup::Robocopy this? I don't see it currently making use of $ENV{PERL_ROBOCOPY_EXE}. It would be good to see that source.

how can I return 0 from the BEGIN block?

I don't think you can... I'm not sure that you need BEGIN blocks in the test code in the first place (as you noted in your update).

There are better ways to do this?

My suggestion would be to not do the checks on $ENV{PERL_ROBOCOPY_EXE} in the main code of the module. It seems to be an OO module, so why not just check for robocopy.exe in new? If you want to give a default that should be modifiable globally, then you could put an our $DEFAULT_ROBOCOPY_EXE = length $ENV{PERL_ROBOCOPY_EXE} ? $ENV{PERL_ROBOCOPY_EXE} : 'C:\\Windows\\System32\\robocopy.exe'; in the main code of Win32::Backup::Robocopy, and then in new, do something along the lines of $self->{robocopy_exe} = $arg{robocopy_exe}//$DEFAULT_ROBOCOPY_EXE;, and then check for the existence of the file $self->{robocopy_exe}.

If this were a non-OO module that has to do some initialization in the main code of the module, then I might abstract out that initialization into a sub, such as sub _init { ... } _init();, as this would allow me to call My::Module::_init() as many times as I want without having to reload the module.