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";

In reply to Re^2: Importing variables from package within same script by moensch
in thread Importing variables from package within same script by moensch

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.