Update:

I found a much more elegant solution. I guess all in our team were all just too tired to see it. Anyway hope it will help someone in the future. The reason I prefer to use the here-to quoting (in favor of something like q[] ) is because it seems likely that there could be problems with the character used for opening and closing any other quoting method, as this character could appear in the quoted text as well.

@EXPORT= qw( XML_FOO XML_BAR ); use constant XML_FOO => <<FOO; #XML CODE HERE FOO use constant XML_BAR => <<BAR; #XML CODE HERE BAR # and so on... # Later in the implementation: $parser->parse_string(XML_FOO);

Original Post

I found a node here 843110 that talks about this but the OP wanted something that was only available at runtime which is not what constants are for, IMHO.

Anyway, I'm lokking for the same thing but to store some XML templates in a lib. And the only way we have got it to work is by using the scalar ref (as they are allowed and documented in the constant pod), like this:

@EXPORT= qw( XML_FOO XML_BAR ); open my $foo, '<', \<<FOO; #XML CODE HERE FOO use constant XML_FOO => \$foo; open my $bar, '<', \<<BAR; #XML CODE HERE BAR use constant XML_BAR => \$bar; # and so on... # Later in the implementation: $parser->parse_string(${XML_FOO()});

My question is twofold. Firstly, I'm guessing this is not the best way because the value of the reference could be changed (not a real a concern in our implementation). Second, are the other or better ways to accomplish this? Answering myself here and I could probably use q() but I wanted to avoid problems with any formatting (e.g. a linefeed before the xml declaration, etc.) the open seemed more elegant. Anyway, seeking opinions!

Thanks,
Alejandro Imass

</code>

In reply to Initialize constant with scalar by ait

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.