Your skill will accomplishwhat the force of many cannot PerlMonks

### Initialize constant with scalar

by ait (Friar)
 on Aug 17, 2012 at 17:11 UTC Need Help??
ait has asked for the wisdom of the Perl Monks concerning the following question:

## 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); [download] ## 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()}); [download] 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> Replies are listed 'Best First'. Re: Initialize constant with scalar by davido (Archbishop) on Aug 17, 2012 at 17:45 UTC use constant XML_FOO => <<FOO; #XML CODE HERE FOO [download] 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';
[download]

Dave

Re: Initialize constant with scalar
by kcott (Canon) 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

Create A New User
Node Status?
node history
Node Type: perlquestion [id://988033]
Approved by kcott
Front-paged by Corion
help
Chatterbox?
 [Corion]: Option a) would mean launching cmd.exe /k c:\path\to\ batchfile- launching-perl- script.cmd. Option b) would be to add pause as the last line of said batch file. [LanX]: First day after holidays ... and already stressed by the fact that colleagues changed stuff without communication ... apparently I'm the only one trying to fight entropy [Corion]: LanX: The command is always in the history if you typed it in before. If you didn't type the command into the command line, it will not be there. I think there is doskey which can stuff command lines into the history LanX damns the cult of CB ;-) LanX WTF WTF WTF [LanX]: please forget my last 3 posts [LanX]: Yeah option a doesn't go into history [LanX]: probably I need to teach the app to restart after C-c Kill [Corion]: LanX: Maybe have an infinite-loop cmd file? Much easier than trying to manage that from within Perl IMO [Corion]: Alternatively, relaunch the application from cron (or a Windows cron) every minute

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (12)
As of 2017-03-27 15:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should Pluto Get Its Planethood Back?

Results (320 votes). Check out past polls.