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

Hi monks,
can you please look at my situation
I have a structure where in all my files are in two
directories /files/gui and /files/db and my cgi files
use the files under these directories .

so i have in all files the above package names

(eg)
package files::db::newfile; use files::db::skills_db qw//;
use files::gui::skills_gui qw//;

Every time i copy these files under different paths to create new set ups

newpath/gui and newpath/db/

i have to change these names in all of my files
(eg)
package newpath::db::newfile; use newpath::db::skills_db qw//;
use newpath::gui::skills_gui qw//;>

My question is i dont want to change these everytime, i
tried setting $ENV{'FOLDER'}="newpath" ,

package $ENV{'FOLDER'}::db::newfile;
use $ENV{'FOLDER'}::db::skills_db qw//;

but it doesnt replace these instead i get errors
Can any one suggest a way to this?

Thanks
  • Comment on Using variables within use and package statement

Replies are listed 'Best First'.
Re: Using variables within use and package statement
by ikegami (Patriarch) on Oct 06, 2005 at 06:32 UTC
    use lib 'newpath'; use gui::skills_gui; use db::skills_db;

    and remove "newpath::" from the package name.

    Note that if the path is relative, it's relative to the current directory, which is not necessarily the same as the directory in which the script resides.

Re: Using variables within use and package statement
by BrowserUk (Patriarch) on Oct 06, 2005 at 06:33 UTC

    See perldoc -m lib (use lib 'DIR').

    In your case, you'd use something like:

    use lib $ENV{'FOLDER'}; use gui::skills_gui; use db::skills_db;

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
      This solution works for use statments , But for package
      name is there an option ?

      package newpath::gui::newfile;

      can newpath be replaced with any variables in defining
      package names?

        Unless I am misunderstanding you, you shouldn't need to do that?

        The 'normal' way of doing things is:

        --- file \this\path\gui\skills_gui.pm -- package skills_gui; ... ---------------------------------------- --- file \this\path\db\skills_db.pm -- package skills_db; ... ---------------------------------------= --- file \that\path\gui\skills_gui.pm -- package skills_gui; ... ---------------------------------------- --- file \that\path\db\skills_db.pm -- package skills_db; ... ---------------------------------------=

        And in the calling script have

        ---- File: \some\other\path\yourscript.pl --- use lib $ENV{LIB_PATH}; use gui::skills_gui; use db::skills_db; ....

        For one run of the script set LIB_PATH=\this\path\ and it will use \this\path\gui\skills_gui.pm and \this\path\db\skills_db.pm
        and for another run you'd set LIB_PATH=\that\path\ and it will use \that\path\gui\skills_gui.pm and \that\path\db\skills_db.pm.

        So the one script unchanged will use either set of modules depending upon the setting of the environment variable.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

        No (unless you want to rewrite your symbol tables, but don't do that).

        This solution works for use statments , But for package name is there an option ?
        None that I know (IIUC). That is: none that is not in the real of brainf***ed solutions. But you don't want to do that: just either use OO syntax directly or export whatever you need to, if you prefer a functional UI.