Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > standard setup for scripts

Reply
Thread Tools

standard setup for scripts

 
 
Tom Bolick
Guest
Posts: n/a
 
      07-26-2007
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...
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      07-27-2007
Tom Bolick wrote:
> 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);
> -----------
>
> Is there a way to put this in a single setup file that all scripts call,
> like setup.pl?


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

BEGIN { do 'setup.pl' }

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
Tom Bolick
Guest
Posts: n/a
 
      07-27-2007
Gunnar Hjalmarsson wrote:

> Tom Bolick wrote:
>
>> 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);
>> -----------
>>
>> Is there a way to put this in a single setup file that all scripts
>> call, like setup.pl?

>
>
> 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?

 
Reply With Quote
 
Tom Bolick
Guest
Posts: n/a
 
      07-27-2007
Petr Vileta wrote:

> Tom Bolick wrote:
>
>> 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);
>> -----------

>
>
> #!/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.
 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      07-27-2007
Tom Bolick <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Gunnar Hjalmarsson wrote:
> > 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
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      07-27-2007
Tom Bolick <(E-Mail Removed)> wrote:

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



You don't need a variable:

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


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      07-27-2007
http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de wrote:
> Tom Bolick wrote:
>> Gunnar Hjalmarsson 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.


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

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Tom Bolick
Guest
Posts: n/a
 
      07-27-2007
Tad McClellan wrote:
> Tom Bolick <(E-Mail Removed)> wrote:
>
>
>>$IS_WEBSITE = (-e "/home/userme/lib/site_perl");
>>if ($IS_WEBSITE){

>
>
>
> 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).

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Python Scripts with IIS - ASP or Python-based CGI scripts withIIS - which makes more sense? davidj411 Python 0 06-27-2008 04:38 PM
What is required for perl scripts to run correct when launched from rc scripts on HPUX 11? deanjones7@gmail.com Perl Misc 13 09-10-2007 11:58 AM
VS 05 Login Control and SQL Server 05 Standard Edition Scripts Rick Gamble ASP .Net 0 05-30-2006 12:09 PM
Stupid question: Making scripts python-scripts Jan Danielsson Python 8 07-22-2005 12:20 AM
Re: Stupid question: Making scripts python-scripts Jp Calderone Python 0 07-21-2005 02:38 PM



Advertisments