Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

use module via variable ? can't get it to work

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
Hi,

I'm trying to set a vaiable to = the name of a module I want to use as follows..

Code:
# Check for required module (windows / *nix)
if($DRIVER eq "ODBC"){
    $MODULE="Win32::ODBC";
}
else{
    $MODULE="DBI";
}

eval "use $MODULE";

All I get when then trying to use the module is...
Can't locate object method "new" via package "Win32::ODBC"

I've tried using
Code:
# Check for required module (windows / *nix)
if($DRIVER eq "ODBC"){
    $MODULE="Win32/ODBC.pm";
}
else{
    $MODULE="DBI.pm";
}

eval "use $MODULE";

and eval with require not use.

Everything is giving me the same results.

What am I missing?

Thanks 1DMF

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
I've also tried..
Code:
# Check for required module (windows / *nix)

my $MODULE;

if($DRIVER eq "ODBC"){
    $MODULE="Win32::ODBC";
}
else{
    $MODULE="DBI";
}

eval {
    "use $MODULE";
    import $MODULE;
};

I get the same problem when issuing..
Code:
 my $db = new Win32::ODBC("DSN=$DSN;UID=$USER_ID;PWD=$PASSWORD") || die "getSQL Error Connecting: " . Win32::ODBC::Error();



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
This might be a stupid question but ...

What happens if you do this:
Code:
use Win32::ODBC;

my $db = new Win32::ODBC("DSN=$DSN;UID=$USER_ID;PWD=$PASSWORD") || die "getSQL Error Connecting: " . Win32::ODBC::Error();
Do you still get the error?


Trojan.
 
nope it's fine if I hard code the module.

But that's no good, the point of this is to dynamically load a module, I've tried every method I can find on the net but nothing seems to work?


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Absolutely.

I wasn't suggesting for one moment that you hard code anything.
I just wanted to be sure that the problem was coming from the failure to "use" the module and not from the "new" or anywhere else.



Trojan.
 
I've found a way round in the end , it seems imposible with use, but require did the job.

even if you wrap a use in a sub & if clause it is still processed so errors if the module doesn't exist... this works however
Code:
sub setDB {

    if($DRIVER eq "ODBC"){
        require "Win32/ODBC.pm";
    }
    else{
        require "DBI.pm";
    }

}

man that was a headache

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
I think that `use` can never be used in an eval, because it's something special in the preprocessing stage in Perl... it reads through all your code and loads every module that you call with "use", and then executes the code after that, and any "use" it finds at this point it ignores.

This is why you can get away with doing this:

Code:
#!/usr/bin/perl -w

print Dumper(\%ENV);

use Data::Dumper;

Even though you use the Dumper method from Data::Dumper before you load Data::Dumper, it still does the right thing because the preprocessor found Data::Dumper before your code began executing. I usually find myself doing this all the time when debugging code; I tend to start typing a "die Dumper" before I realize that Data::Dumper probably isn't loaded in the current code I'm working with, so I just add "use Data::Dumper" after I call Dumper.

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
there is a pragma called "if" that allows you to conditionally load modules but I am not familiar with its usage. Might be new for perl 5.10

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
(sorry Kevin, cross-posted... yes, I did take over 2 hours to submit that post!! :) )

Annihilannic.
 
I tried all those 'eval' methods none seemed to work.

oh well my final code did the trick and if it aint broke ;-)

took me a while though, conditionally loading modules doesn't seem like a strange request. Why is it so damn hard to do ?

I guess Kirsle hits the nail on the head, it's to do with the special way 'use' is pre-compiled compared to the 'require' statement.

From what I read 'use' is basically a 'require' wraped in a begin and the import statement, but I read so much crap trying to find the answer and most didn't work I don't what was true /correct and what wasn't.


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Its not hard to do. I assume you didn't know the difference between "compile" and "run" times and the difference between "use" and "require". Now that you know you can load modules conditionally all you want.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
ooh put ya claws away Kevin :p

yes I know the difference between compile time & run time, what I didn't know was that is the difference between 'use' and 'require'.

I though 'require' was for 'requiring' -> files and 'use' was for 'using' -> modules.

not that 'use' was some stupid wrapper for a require which was inperpreted at compile and not run time.

But now I do, I will be 'requiring' modules I want dynamically but 'using' modules that are static.

Someone needs to tell the 101 posts I found all claiming you can use the 'eval' against a 'use' to perform this.





"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top