Re: retrieving a constant from a dynamically loaded module



olivier.grant@xxxxxxxxx wrote:

[snip!]

the following line of code

   eval("print ".$module."::TSD_VERSION;");
   eval("print(\"module version
v\".".$ScriptData{'CoreModule'}."::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{'CoreModule'}.\"
v\".".$ScriptData{'CoreModule'}.'::TSD_VERSION'.".\"
(\".".$ScriptData{'CoreModule'}.'::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{CoreModule}' v" .
    eval {"$ScriptData{CoreModule}::TSD_VERSION"} .
    eval {"$ScriptData{CoreModule}::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{CoreModule} v" ...

Tom Wyant
.