undyingking: (Default)
undyingking ([personal profile] undyingking) wrote2008-01-01 05:28 pm
Entry tags:

Manipulating Perl include path

I have a wee problem in a current project. My code uses the XML::Parser module, and it needs to be a recent version. So I've uploaded this to the server and am adding it to the include path with the help of a use lib in the normal sort of way.

Unfortunately, elsewhere on the server there seems to be an old version of the module, and it seems to be using this one in preference to my nice shiny new one.

Is there any way I can, at run-time, compel it to look for the module only in my preferred part of the include path? Maybe by monkeying with whatever environment variable it is that controls the said path?

I thought of renaming the version of the module that I uploaded, and using it under the new name, but that seems a bit drastic. But maybe it's not really.
chrisvenus: (Default)

[personal profile] chrisvenus 2008-01-01 05:32 pm (UTC)(link)
http://perldoc.perl.org/functions/use.html implies that you can supply a version number too. Does that help at all (just thinking that if it finds one in the path that isn't the reight version it might keep looking til it finds one that is the right version). Oterwise I have no idea how you might go about this. :)

[identity profile] undyingking.livejournal.com 2008-01-01 06:55 pm (UTC)(link)
Mm, that looks quite hopeful, thanks! That doc applies to Perl 5.10 while mine is 5.005, but I'll have a scout around and see if it was available back then too.

[identity profile] thefon.livejournal.com 2008-01-02 05:59 am (UTC)(link)
I think it's all about manipulating the @INC array.
http://www.perlhowto.com/extending_the_library_path

[identity profile] e-pepys.livejournal.com 2008-01-02 11:18 am (UTC)(link)
I do it by reordering the @INC array:

BEGIN {unshift @INC, "/directory/where/you/put/it"}

The unshift puts the new directory at the beginning of the list, so it'll find that one first.

Alternatively, you can invoke perl with the -I option, which adds to the beginning of @INC.

Or you can use lib like you are doing, which also adds the path to the start of the list. This is normally the best way if you just have a simple directory path to add.

So, why doesn't it work for you? Are you sure you are specifying the directory containing the XML directory, not the XML directory itself? It adds XML/Parser.pm to each directory in the path.

[identity profile] e-pepys.livejournal.com 2008-01-02 11:25 am (UTC)(link)
PS. to answer your question about compelling it to only load the one you ask for: you can do this with

BEGIN {require "/path/XML/Parser.pm"; import XML::Parser}

However that will still use @INC for anything that XML::Parser uses, and it looks like the package has various things that come with XML::Parser.

[identity profile] undyingking.livejournal.com 2008-01-02 12:19 pm (UTC)(link)
Yeah, there's a monstrous great dependency tree alas.

Since posting, I've got round it by writing my own XML parser for just the bits I needed (!), but I shall bear your @INC suggestions in mind for the future, thanks!