Click here to get back home

retrieving a constant from a dynamically loaded module

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
retrieving a constant from a dynamically loaded module olivier.grant 10-26-2005
Posted by olivier.grant on October 26, 2005, 3:29 am
Please log in for more thread options


Hi All,

I need to be able to dynamically load a module and retrieve a constant
from it. Simply put, i want to allow a user to specify a module
HisModule.pm containing the package HisModule and get
HisModule::VERSION.

in his file :
[code]
package HisModule;
use constant VERSION;
1;
[/code]

in my code :
[code]
$module = shift @ARGV;
print " Using module v".$module::VERSION."\n";
[/code]

I know this is not the right syntax, but I can't seem to find it .. Is
it even possible to do this ?

Thanks,

Olivier



Posted by Gunnar Hjalmarsson on October 26, 2005, 12:36 pm
Please log in for more thread options


olivier.grant@gmail.com wrote:
> I need to be able to dynamically load a module and retrieve a constant
> from it. Simply put, i want to allow a user to specify a module
> HisModule.pm containing the package HisModule and get
> HisModule::VERSION.
>
> in his file :
> [code]
> package HisModule;
> use constant VERSION;
> 1;
> [/code]
>
> in my code :
> [code]
> $module = shift @ARGV;

chomp $module;
require $module;

> print " Using module v".$module::VERSION."\n";
> [/code]

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


Posted by olivier.grant on October 26, 2005, 5:12 am
Please log in for more thread options


I got that part, but the problem is that as standard, each module and
script has a constant called "TSD_VERSION", so I need to be able to
specify that the TSD_VERSION constant i'm looking for is the one from
the module specifyed by the user, so the syntax should look something
like this :

$module::TSD_VERSION;

but that doesn't work. wrapping this statement in an eval will work,
but several problems pop in :
the following line of code

eval("print ".$module."::TSD_VERSION;");
eval("print(\"module version
v\".".$ScriptData."::TSD_VERSION);");

will print the correct version number, but if you add a bit more to the
line to output doesn't work :

eval("Logger::Print(\"Using core module \".$ScriptData.\"
v\".".$ScriptData.'::TSD_VERSION'.".\"
(\".".$ScriptData.'::TSD_LAST_UPDATE'.".\")\n\");");

(sorry for the obscure code)



Posted by harryfmudd [AT] comcast [DOT] on October 26, 2005, 11:03 am
Please log in for more thread options


olivier.grant@gmail.com wrote:

[snip!]

> the following line of code
>
> eval("print ".$module."::TSD_VERSION;");
> eval("print(\"module version
> v\".".$ScriptData."::TSD_VERSION);");
>
> will print the correct version number, but if you add a bit more to the
> line to output doesn't work :
>
> eval("Logger::Print(\"Using core module \".$ScriptData.\"
> v\".".$ScriptData.'::TSD_VERSION'.".\"
> (\".".$ScriptData.'::TSD_LAST_UPDATE'.".\")\n\");");
>
> (sorry for the obscure code)
>

Well, there's something to be said for the K.I.S.S. principle. Why build
a big complicated string if you can call Logger::Print directly, and
just eval() the part that needs eval()-ing? Something like

Logger::Print ("Using core module '$ScriptData' v" .
eval ::TSD_VERSION"} .
eval ::TSD_LAST_UPDATE} . "\n");

seems clearer to me.

If you must build a big complicated string, have you printed it before
eval()-ing it, to see what you get? You might also consider using the
"qq{}" construct, so you don't have to escape all the embedded quotes.
Note that in my cold-coded simplification I changed your output by using
the "'" character. If you _must_ have '"', consider Logger::Print
(qq{Using core module "$ScriptData v" ...

Tom Wyant


Similar ThreadsPosted
Q: Digest::SHA1 module cannot be loaded February 8, 2005, 5:50 pm
constant STILL_ALIVE June 6, 2006, 3:09 pm
Retrieving information from website July 28, 2006, 4:47 pm
mod_perl is in .conf file, but how to verify it's loaded? February 23, 2007, 4:59 pm
Net::Telnet retrieving temporary text problem November 6, 2006, 3:32 am
http, library loaded, httpS library can't be found November 8, 2004, 9:51 am
Retrieving XML attribute using XML::XPath::Node::Attribute June 28, 2005, 5:02 am
Lower case module name for non-pragma module January 4, 2005, 10:19 am
RFC: New module 'Module::Bundled::Files' August 26, 2005, 3:49 pm
help with an MD5.pm module!! September 3, 2005, 11:39 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap