Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re: Error: Attempt to reload module.. while testing failing require by haukex
in thread Error: Attempt to reload module.. while testing failing require by Discipulus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (4)
    As of 2019-08-24 22:18 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?