standard setup for scripts

Discussion in 'Perl Misc' started by Tom Bolick, Jul 27, 2007.

  1. Tom Bolick

    Tom Bolick Guest

    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...
    Tom Bolick, Jul 27, 2007
    #1
    1. Advertising

  2. 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
    Gunnar Hjalmarsson, Jul 27, 2007
    #2
    1. Advertising

  3. Tom Bolick

    Tom Bolick Guest

    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?
    Tom Bolick, Jul 27, 2007
    #3
  4. Tom Bolick

    Tom Bolick Guest

    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.
    Tom Bolick, Jul 27, 2007
    #4
  5. Tom Bolick

    -berlin.de Guest

    Tom Bolick <> 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
    -berlin.de, Jul 27, 2007
    #5
  6. Tom Bolick <> 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/"
    Tad McClellan, Jul 27, 2007
    #6
  7. -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
    Gunnar Hjalmarsson, Jul 27, 2007
    #7
  8. Tom Bolick

    Tom Bolick Guest

    Tad McClellan wrote:
    > Tom Bolick <> 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).
    Tom Bolick, Jul 27, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jack Orenstein

    distutils setup ignoring scripts

    Jack Orenstein, Mar 15, 2005, in forum: Python
    Replies:
    0
    Views:
    255
    Jack Orenstein
    Mar 15, 2005
  2. Jan Danielsson
    Replies:
    8
    Views:
    610
    Mike Meyer
    Jul 22, 2005
  3. Jp Calderone
    Replies:
    0
    Views:
    443
    Jp Calderone
    Jul 21, 2005
  4. davidj411
    Replies:
    0
    Views:
    481
    davidj411
    Jun 27, 2008
  5. Replies:
    13
    Views:
    513
    Anno Siegel
    Sep 10, 2007
Loading...

Share This Page