Dan Muey wrote: > Howdy list! > > Within a module function that can be exported I use a function, say > header() from CGI. > IN that module I have to have > use CGI qw(header); > OK super. > What about if I have a function I export that uses a function from > say, the DBI module, and the > first argument sent to it is the Data Base Handle Variable like so: > > Script: > use DBI; > use MyFunkyModule qw(databasestuff); > my $dbh DBI->connect..... > > print databasestuff($dbh,$query); > > MyFunkyModule.pm: > > sub databasestuff { > my $dbh = shift; > my $query = shift; > return $dbh->do($query); > } > (I realize that in this case I might As well just do do() but it's > just an example to illustrate the idea) > > Would I still have to have use DBI; in the package MyFunkyModule; in > MyFunkyModule.pm?
No, not in the example you've shown. > > Basically I'd like to export functions that use uncommon modules. > I'd like those functions to fail with an error if they don't have > them installed or give they are given a bad object, IE like if $dbh > was a simple string in my example above.. > > I supposed I could do and eval on the module inside the function: > > sub databasestuff { > eval { use DBI; }; You have to use "require" instead of "use" in a case like this, because "use" is a compile-time thing. > die "You need DBI installed to use this function $@" if($@); > my $dbh = shift; > my $query = shift; > return $dbh->do($query); > } > (Yes I know DBI is a standard module, I'm just using it as an example > since most people know how to use it) > > But if the just sending $dbh part of the object would work so I > wouldn't have to use() it in MyFunkyModule.pm that'd be great. If you want to know if $dbh is actually a database handle, you can use: die "Not a DBI database handle" unless UNIVERSAL::isa($dbh, 'DBI::db'); You can also check to see if $dbh supports a "do" method before calling it: die "Object doesn't have a 'do' method" unless UNIVERSAL::can($dbh, 'do'); This won't work if do is an autoloaded method (which it isn't in DBI). In that case, wrapping the method call in eval() is the way to go. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]