in reply to Re: Importing variables from package within same script
in thread Importing variables from package within same script
Thank you very much. This is all working as it should now.
May I be cheeky and further the question by explaining the actual problem I am (was) trying to solve with this?
I have a program which uses XML::LibXML and some of its constants (e.g. XML_ELEMENT_NODE). I want to modify the program to make it work on systems which do not have XML::LibXML installed and just make the functionality which require this module unavailable. The problem I faced: Even though I could put the 'require & import XML::LibXML' statements into an eval block, the compiler still moaned about me using Barewords (the now non-available XML::LibXML::Common constants).
I have solved this problem as follows now, but am wondering if there is a "cleaner" approach to this rather than just importing "fake" constants from another package with a zero value instead.
#!/usr/bin/perl -w BEGIN { package LibXMLVars; require Exporter; our @ISA = qw(Exporter); our @EXPORT_OK = qw(XML_ELEMENT_NODE XML_ATTRIBUTE_NODE XML_TE +XT_NODE XML_CDATA_SECTION_NODE); use constant XML_ELEMENT_NODE => 0; use constant XML_ATTRIBUTE_NODE => 0; use constant XML_TEXT_NODE => 0; use constant XML_CDATA_SECTION_NODE => 0; } package main; use strict; BEGIN { eval { require XML::LibXML; import XML::LibXML; require XML::LibXML::Common; import XML::LibXML::Common qw(:libxml); }; if ($@) { warn "Failed to import XML::LibXML"; import LibXMLVars qw(XML_ELEMENT_NODE XML_ATTRIBUTE_NO +DE XML_TEXT_NODE XML_CDATA_SECTION_NODE); } } print "Test: '".XML_ELEMENT_NODE."'\n";
|
|---|