CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Need help writing a subroutine to detect if a module is installed

 



Spork Schivago
New User

Jan 22, 2016, 5:29 PM

Post #1 of 2 (1140 views)
Need help writing a subroutine to detect if a module is installed Can't Post

Hello. I'm very new to Perl programming. I'm not really trying to learn the language so much, just trying to modify a perl script to improve on it a bit. I'm fairly good with C and a few other languages out there.

I'd like to check to see if various perl modules are installed inside a script and not have it error out during compilation. I can do this just fine, until I try to create a subroutine / function. The whole idea of the subroutine / function was so I wouldn't have to write the same code multiple times. Could someone please show me what I'm doing wrong and give me a suggestion on how to fix it?

Here's my code:

Code
sub check_depends { 
my $mod = shift;
if (eval {require $mod;1;} ne 1) {
# can't find the module
print "We require $mod module to be installed in order to use this script.\n";
} else {
# we found it!
print "The $mod module is installed!\n";
}
}

check_depends(IO::Socket::SSL);


If I replace this line:

Code
if (eval {require $mod;1;} ne 1) {

With this line:

Code
if (eval {require IO::Socket::SSL;1;} ne 1) {

It works as expected. The IO::Socket::SSL module is installed, but with the subroutine / function, it shows it's not.

I believe the problem might be with the eval statement. Maybe I can't use variables inside of it like I'm doing? Any help would be greatly appreciated. Thank you!


Spork Schivago
New User

Jan 22, 2016, 5:42 PM

Post #2 of 2 (1138 views)
Re: [Spork Schivago] Need help writing a subroutine to detect if a module is installed [In reply to] Can't Post

I do believe I fixed my problem. If anything thinks this is the incorrect way of doing what I'm trying to do, please let me know.


Code
sub check_depends { 
my $mod = shift;
if (eval "require $mod;1;" ne 1) {
# can't find the module
print "We require $mod module to be installed in order to use this script.\n";
} else {
# we found it!
print "The $mod module is installed!\n";
}
}

check_depends(IO::Socket::SSL);


This now seems to work as expected. Thanks!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives