Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Problem with conditional module load

by pgduke65 (Acolyte)
on Feb 13, 2015 at 20:27 UTC ( [id://1116660]=perlquestion: print w/replies, xml ) Need Help??

pgduke65 has asked for the wisdom of the Perl Monks concerning the following question:

I am obviously missing something simnple here. But I cannot seem to find it. Please help me to understand what I am doing incorrectly.

The Goal: Write a module that will conditionally include other modules as needed based on the current o/s.

Idea calling script includes Module Security. Module Security then includes either Windows module or Unix module depending on the $^O variable value.

I have searched here and googled and have tried the following:,

Test Calling Script

use strict; use warnings; use lib 'C:/Perl'; use SecurityV1; printf "Hello World\n";

Security Module Code

package SecurityV1; BEGIN { my $LoadModule = ($^O ne 'MSWin32') ? 'UnixSecurity' : 'Win32Secur +ity'; eval "require $LoadModule; 1;" or die $@ if $LoadModule; $LoadModule->import(); } use strict; use warnings; use constant { TRUE => 1, FALSE => 0 }; use Exporter; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK ); our $VERSION = 1.00; our @ISA = qw(Exporter); @EXPORT = qw(); @EXPORT_OK = qw(); Test(); 1;

Win32Security Module

package SecurityWin32; use strict; use warnings; use constant { TRUE => 1, FALSE => 0 }; use Exporter; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK ); our $VERSION = 1.00; our @ISA = qw(Exporter); @EXPORT = qw(Test); @EXPORT_OK = qw(); sub Test { printf "Loading %s\n", __PACKAGE__; } 1;

UnixSecurity Module

package SecurityUnix; use strict; use warnings; use constant { TRUE => 1, FALSE => 0 }; printf "Loading %s\n", __PACKAGE__; use Exporter; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK ); our $VERSION = 1.00; our @ISA = qw(Exporter); @EXPORT = qw(Test); @EXPORT_OK = qw(); sub Test { printf "Loading %s\n", __PACKAGE__; } 1;

The Error:

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 (eval 1) line 1. BEGIN failed--compilation aborted at C:/Perl/SecurityV1.pm line 38. Compilation failed in require at C:\Perl\TestLoad.pl line 5. BEGIN failed--compilation aborted at C:\Perl\TestLoad.pl line 5. C:\Perl>

The C:\Perl Directory contains all modules and the calling script.>

Replies are listed 'Best First'.
Re: Problem with conditional module load
by Anonymous Monk on Feb 13, 2015 at 21:22 UTC

    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 ....

      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

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1116660]
Approved by Old_Gray_Bear
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-23 11:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found