Am 19.12.2013 19:27, schrieb Rick T:
The following three lines are from a program that works fine for me.

        # Choose template file
        use constant TMPL_FILE => 
"/big/dom/xoldserver/www/templates/open_courses3.html";
        my $tmpl = new HTML::Template( filename => TMPL_FILE );

I wanted to make the program more portable, which means changing the name of 
the server. My idea was to put the following

        my $server = “newserver”

at the beginning of the code, then have the later lines of code use this 
string. That way I’d only have to change the one line when moving to another 
server. What I tried first was

        use constant TMPL_FILE => "/big/dom/x” . $server . 
“/www/templates/open_courses3.html";

This gave me a syntax error, as did several variants I tried. I looked up “use 
constant” in Learning Perl but found the discussion over my head (I’m a 
beginner!), so I was hoping someone could explain a correct way to write this 
code if there is one.

use is interpreted at first,
while the normal my $var = 'xyz' declaration is interpreted at runtime.

So when you use use, no variable beside other constants are set.

try instead

use constant SERVER => 'Newserver';  # probably better written with ss
use constant TMPL_FILE => '/big/dom/x' . SERVER . '/www/templates/opencourses3.html'


Greetings,
Janek

PS:
* if you lookup perldoc use,
you'll see the use Module is equivalent to
BEGIN { require Module }
and the BEGIN blocks are run first of all.

PPS:
IMHO, it's better and more convinient to rely on conventions instead of rules, so I just
write constants as $CONSTANT = ....;
and just never change them ever again.
But it still makes it easier to make stuff like you did and probably more important, we can interpolate them into a string easier, like
print "blablabla $CONSTAT foofofofo";
Well, but that's just a matter of taste of course.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to