Mo Beigi
Mo Beigi

Reputation: 1765

Can't include JSON::XS module locally in CGI perl script but can include JSON module

So for a particular CGI perl script I have included JSON like this to handle some .json files:

use lib "./modules/JSON/lib";
use JSON;

This works fine and well. The web directory holds the files required in the modules folder.

However, the JSON module is very slow. I read that JSON:XS can be much, much faster but I can't seem to simply use it as so:

use lib "./modules/JSON-XS";
use JSON::XS;

There is no lib folder in the JSON-XS files, i've tried combinations of use (ie, using both folders and etc) but it didn't work.

And no I cannot simply install the module for this particular project.

Any help is appreciated.

Upvotes: 0

Views: 936

Answers (3)

Mo Beigi
Mo Beigi

Reputation: 1765

Okay this finally worked for me:

I did this process to all the dependencies (in the order of no dependencies to more dependencies)

export PERL5LIB = ~/path/to/modules/perl5
perl Makefile.PL PREFIX=$PERL5LIB LIB=$PERL5LIB
make
make test
make install

This installed all modules into a directory I called perl5. It also means that when you try to install other modules locally the dependencies issue does not appear due to the PREFIX/LIB additions.

Then all I did was add this to my perl CGI script:

use lib "./modules/perl5";
use JSON::XS;

PS: JSON::XS is so much faster!

:D

Upvotes: 0

Slaven Rezic
Slaven Rezic

Reputation: 4581

Perl distributions are usually usable in an uninstalled state. What you just need to do is to call perl Makefile.PL && make (or for a Module::Build-based distribution: perl Build.PL && ./Build). This will do all necessary compilations (if it's an XS module) and copy the library files into the blib subdirectory. In your script instead of use lib you would write use blib:

use blib "/path/to/JSON-XS";

Note that if a module has dependencies, then you have to resolve it yourself and add that many use blib statements. JSON::XS does not have that many dependencies, but it will be really inconvenient for other modules. In this case you should probably seek another solution, e.g. using CPAN.pm together with local::lib.

Upvotes: 1

ikegami
ikegami

Reputation: 385829

And no I cannot simply install the module for this particular project.

You can't use a module without installing it. You've just been getting away with doing a half-assed job of it. That won't work for JSON::XS, though. The reason it's fast is because it's written in C, so you'll need to compile the C code. The easiest way by far to do this is to use the provided installer instead of reinventing the wheel.

(You do know you can install a module into any directory, and that this does not require special permissions, right?)

Upvotes: 2

Related Questions