standard setup for scripts

T

Tom Bolick

All of my perl scripts have this at the top:
-------
#!/usr/bin/perl

$IS_WEBSITE = (-e "/home/userme/lib/site_perl");

if ($IS_WEBSITE){
use lib "/home/userme/lib/site_perl";
$ENV{HTML_TEMPLATE_ROOT}='/home/userme/www/project1/online/eng';
}
else {
$ENV{HTML_TEMPLATE_ROOT}='/WWWSites/client1/project1/online/eng';
}
$ENV{HEAD_BASE}='../project1/online/eng/';

use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser set_message);
-----------

Is there a way to put this in a single setup file that all scripts call,
like setup.pl?

As you can see, I check to see if I am running on my production server,
or my development machine and change some variables and libraries
accordingly. Anyone have any better suggestions?

Tom...
 
G

Gunnar Hjalmarsson

Tom said:
All of my perl scripts have this at the top:
-------
#!/usr/bin/perl

$IS_WEBSITE = (-e "/home/userme/lib/site_perl");

if ($IS_WEBSITE){
use lib "/home/userme/lib/site_perl";

It should be noted that the 'use lib' statement is always executed,
since it happens at compile time. OTOH, even if that path is added to
@INC unnecessarily, i.e. also when it does not exist, it won't hurt.
$ENV{HTML_TEMPLATE_ROOT}='/home/userme/www/project1/online/eng';
}
else {
$ENV{HTML_TEMPLATE_ROOT}='/WWWSites/client1/project1/online/eng';
}
$ENV{HEAD_BASE}='../project1/online/eng/';

use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser set_message);

Guess you could put all but the first line in a separate file and call
it like:

BEGIN { do 'setup.pl' }
 
T

Tom Bolick

Gunnar said:
It should be noted that the 'use lib' statement is always executed,
since it happens at compile time. OTOH, even if that path is added to
@INC unnecessarily, i.e. also when it does not exist, it won't hurt.



Guess you could put all but the first line in a separate file and call
it like:

BEGIN { do 'setup.pl' }

But will this work with strict? Any variables defined in setup.pl are
not defined in the script that calls it? Or am I wrong?
 
T

Tom Bolick

Petr said:
#!/usr/bin/perl

use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser set_message);
require 'setup.pl';
use vars qw($IS_WEBSITE);
...

--- file setup.pl ---
$IS_WEBSITE = (-e "/home/userme/lib/site_perl");
if ($IS_WEBSITE){
use lib "/home/userme/lib/site_perl";
$ENV{HTML_TEMPLATE_ROOT}='/home/userme/www/project1/online/eng';
}
else {
$ENV{HTML_TEMPLATE_ROOT}='/WWWSites/client1/project1/online/eng';
}
$ENV{HEAD_BASE}='../project1/online/eng/';
1;
--- end of file ---

Variables like @ENV, @ARGV, @INC are always global (Perl internal).

Sounds perfect. I assume I can't stuff all my 'use' statements into a
script. I have up to 10 sometimes.

I was using require 'config.pl' for config variables, but I'm trying to
convert all my scripts to strict, so I'm using Config::General now. I
guess I could 'use vars' for that, but Config seems cleaner.

Thanks for the help.
 
A

anno4000

Tom Bolick said:
Gunnar said:
Tom Bolick wrote:
[...]
Guess you could put all but the first line in a separate file and call
it like:

BEGIN { do 'setup.pl' }

But will this work with strict? Any variables defined in setup.pl are
not defined in the script that calls it? Or am I wrong?

You are. Did you try it?

The one thing that won't have an effect outside setup.pl is "use
strict". Put that (and "use warnings") in the main script.

Anno
 
T

Tad McClellan

Tom Bolick said:
$IS_WEBSITE = (-e "/home/userme/lib/site_perl");
if ($IS_WEBSITE){


You don't need a variable:

if ( -e "/home/userme/lib/site_perl"){
 
G

Gunnar Hjalmarsson

You are. Did you try it?

The one thing that won't have an effect outside setup.pl is "use
strict". Put that (and "use warnings") in the main script.

Right, Anno, I missed that 'use strict' is lexically scoped.
 
T

Tom Bolick

Tad said:
You don't need a variable:

if ( -e "/home/userme/lib/site_perl"){

Understood. But I use $IS_WEBSITE to know which database to attach to
later in my code. And while I could use the same check elsewhere, I
wanted the path specific stuff in as few places as possible (should it
ever change).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top