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


in reply to Problem with conditional module load

Assuming your files are named "SecurityWin32.pm" and "SecurityUnix.pm", the reason you're getting that error is because you're trying to load modules named "Win32Security" and "UnixSecurity".

BTW, as an alternative for conditional loading of modules, see use if ....

Replies are listed 'Best First'.
Re^2: Problem with conditional module load
by QM (Parson) on Feb 17, 2015 at 11:59 UTC
    To be blatantly obvious for the sake of posterity, use Module goes out to the file system looking for a file named Module.pm in the search path.

    You've named your packages one way, and your files another.

    The package named in Module.pm could be called anything, and indeed, multiple packages (and more use Another::Module statements) may be present. If you want to do the Perl equivalent of putting your coat on front-to-back, be prepared for awkwardness (and the odd chuckle from the peanut gallery).

    But we've all done silly, nay, even stupid, stuff before. Some of us even admit to it. Welcome to the club! I hear the members are of the highest quality!

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

Re^2: Problem with conditional module load
by pgduke65 (Acolyte) on Feb 16, 2015 at 14:35 UTC

    Thank you so much for the reply. I have looked things over and modified the code to run with the use if pragma:

    #BEGIN { # my $LoadModule = ($^O ne 'MSWin32') ? 'UnixSecurity' . '..pm' : ' +Win32Security' . '..pm'; # eval "require $LoadModule; 1;" or die $@ if $LoadModule; # $LoadModule->import(); # #} use if ($^O eq 'MSWin32'), 'Win32Security'; use if ($^O ne 'MSWin32'), 'UnixSecurity';

    I am still getting the error and I am not sure why?

    C:\Perl>TestLoad.pl Can't locate Win32Security.pm in @INC (you may need to install the Win32Security module) (@INC contains: C:/Perl C:/strawberry/perl/site/lib C:/strawberry/perl/ vendor/lib C:/strawberry/perl/lib .) at C:/strawberry/perl/lib/if.pm line 13. BEGIN failed--compilation aborted at C:/Perl/SecurityV1.pm line 40. Compilation failed in require at C:\Perl\TestLoad.pl line 5. BEGIN failed--compilation aborted at C:\Perl\TestLoad.pl line 5.

    Based on my current understanding, I believe the revised code should work.

      I am still getting the error and I am not sure why?

      The same reason as before.

        Ok, I am very confused?

        Based on prior replies, I have added the .pm to the filenames and now the error is stating:

        Can't locate Win32Security.pm.pm in @INC (you may need to install the Win32Secur ity.pm module) (@INC contains: C:/Perl C:/strawberry/perl/site/lib C:/strawberry /perl/vendor/lib C:/strawberry/perl/lib .) at C:/strawberry/perl/lib/if.pm line 13. BEGIN failed--compilation aborted at C:/Perl/SecurityV1.pm line 40. Compilation failed in require at C:\Perl\TestLoad.pl line 5. BEGIN failed--compilation aborted at C:\Perl\TestLoad.pl line 5.

        I sincerely appreciate the help. But I am obviously missing what you are pointing out