I have a CGI interface that calls a Perl plugin. Those scripts are located in different folders and the Perl one is supposed to read an XML file that is located in another folder. To make it simple I give you an example:
/a/b/cgi-bin/test.cgi # calls test.pl
/e/plugins/test.pl # reads config.xml
/e/etc/config.xml
Now I don’t know how to make the Perl plugin to read the config file. Ofcourse I can’t write the path on the script because tomorrow I may change the location of this file. I tryed using Cwd and abs_path on test.pl but this method returns the “running” path when instead I want the “location” path of test.pl so I can modify it in order to build the config.xml path. Any ideas?
EDIT: Using realpath instead of abspath solved my problem, or at least it seems so, I’ll do more tests.
EDIT2: Ok realpath works but now I have another problem… The Perl plugin uses some additional packages that I made but ofcourse now writing use MyPackage; doesn’t work anymore because it looks on the wrong folder… Is there a way to modify the environment variable? I don’t know something that will avoid me from changing thousands lines of code.
EDIT3: Ok solved that too somehow, I guess I will just install my packages in the INC folder so I don’t have to mess up with my code anymore.
The following function will return the path of an
used()ed package:So if test.pl implements a package, say
Plugin::Foo, then just call it as:(Assuming that I’ve understood your folder structure correctly).
Updated: to use abs_path.
I often include this method in my own modules (replacing
shiftwith__PACKAGE__) so that they can find any config/data they require.