frinky has asked for the wisdom of the Perl Monks concerning the following question:

Hi there,

First, the situation:
I'm finally convinced (by Ovid's article) to change all my work to use 'strict' and 'perl -Tw'.
So I tried...

Next, the problem:
I've got a whole bunch of vars (like $rootpath, $rooturl, $sitename, $cgipath, etc. etc.) and some subroutines (like make_html, fatal_error, etc.) stuffed into two files, which are called by 'require "vars.pl" ' and 'require "sub.pl"'.
As you probably know, those vars aren't taken into the main-cgi-file, and there is my problem.
A thread like this has started earlier on, but all I found there terrified me: do I really have to change every line like
'print "path to cgi is $cgi_path";' into
'print "path to cgi is $whatever::$cgi_path";' ???
I'd have to do 50 hours of work if that's indeed the only possibility...

Last, the solution:
?

Might you know it, please let me know!!!

  • Comment on changing to strict-- problems with 'require files'

Replies are listed 'Best First'.
Re: changing to strict-- problems with 'require files'
by erikharrison (Deacon) on Apr 23, 2002 at 13:49 UTC

    Actually, it's $whatever::cgi_path. A couple of slutions:

    1. Automate the changing process. Write a Perl program that uses regexes to chage all instances of these vars into there fully gualified version
    2. use Exporter and export these symbols into the main package and then use our. Both of these things are so useful if you don't know how to use them, learning would benefit elsewhere.

    I personally would use the latter, as it requires the least typing and effort, while at the same time being more guarenteed of success.

    Good luck!

    Cheers,
    Erik
      Thanx for the advice, you're absolutely right about the $whatever::cgi_path.
      And also about the possibility to automate the vars-change, but it would still be pretty annoying to write every new line with '$whatever::' before '$some_var' (without the '$' in that case)
      I'll go look for some explanation about Exporter.
      Thanx for your help!
Re: changing to strict-- problems with 'require files'
by maverick (Curate) on Apr 23, 2002 at 17:38 UTC
    Along with erikharrison's suggestion... It seems like the you have general config type variables. The next step (after you get strict, etc working) might be to take a shot at using one of the Config:: modules from CPAN.

    If this is a program that's going to distrubuted to others, or the variables might be set by someone other than you...then it might be worth your time.

    Consider that by using a config file instead of a .pm you could have:

    • param = value type syntax (for those that don't understand my $foo = "bar";
    • validation of the parameters. I *think* some of those modules have support for that.
    • very litle chance of inducing a compilation error. if someone forgets the ';' at the end of the line, what happens to your program?

    /\/\averick
    OmG! They killed tilly! You *bleep*!!