Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Initialize constant with scalar

by ait (Hermit)
on Aug 17, 2012 at 17:11 UTC ( #988033=perlquestion: print w/replies, xml ) Need Help??

ait has asked for the wisdom of the Perl Monks concerning the following question:


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!

Alejandro Imass


Replies are listed 'Best First'.
Re: Initialize constant with scalar
by davido (Cardinal) on Aug 17, 2012 at 17:45 UTC
    use constant XML_FOO => <<FOO; #XML CODE HERE FOO

    While you're at it, wrap FOO in non-interpolating quotes so that if your XML contains something that looks like an identifier you don't get a "Global symbol "$xyz" requires explicit package name at..." error.

    use constant XML_FOO => <<'FOO';


Re: Initialize constant with scalar
by kcott (Bishop) on Aug 17, 2012 at 18:07 UTC

    G'day, ait,

    You may be interested in Perl Best Practices Chapter 4 - Here Documents (pp. 61-65). You've just reinvented the theredoc! Well done ++ :-)

    As an adjunct to this, you may find Re: Use of Constants of interest.

    -- Ken

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://988033]
Approved by kcott
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2019-11-21 03:19 GMT
Find Nodes?
    Voting Booth?
    Strict and warnings: which comes first?

    Results (103 votes). Check out past polls.