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

Here's the deal:
I have many perl scripts I want to run in a specific order and this is accomplished by using a main (perl) script calling system() for every one of the scripts. Many of these scripts contain require()s or other kind of references to files and they are made using relative paths (../bar instead of /the/whole/directory/structure/bar). However, when calling such scripts from a main script instead of running them on their own, the relative paths won't work. I guess this is the same phenomenon as calling the script manually from a directory other than it's own.

Is there a way of fixing or avoiding the problem, other than changing every file path to an absolute one? I'm hoping there is something like system(./dir/foo.pl, BEHAVE_AS_IF_THE_SCRIPT_WAS_CALLED_FROM_ITS_OWN_DIR)

Any possible help is appreciated

  • Comment on system("./foo.pl") and a problem with relative paths in foo.pl

Replies are listed 'Best First'.
Re: system("./foo.pl") and a problem with relative paths in foo.pl
by roboticus (Chancellor) on Nov 20, 2009 at 13:32 UTC
    dellnak:

    Update your calling script to switch to the appropriate directory before calling the script, like:

    chdir("./dir"); system("./foo.pl");

    ...roboticus

    Error checking left as an exercise for the reader.

      Thanks to you (and cdarke equally)!

      Really basic, I am ashamed. But it works and therefore it was worth it.

Re: system("./foo.pl") and a problem with relative paths in foo.pl
by cdarke (Prior) on Nov 20, 2009 at 13:27 UTC
    While I'm not suggesting this is elegant, the following works on Linux:
    system('cd mydir;./gash.pl');
    Where gash.pl lives in mydir.
Re: system("./foo.pl") and a problem with relative paths in foo.pl
by MidLifeXis (Monsignor) on Nov 20, 2009 at 16:21 UTC