in reply to Can not use JSON module

Following up on what tobyink and 1nickt said, in a recent distribution I wrote, I let the compiler decide whether to use JSON (XS) or JSON::PP which is already in the core. If JSON is not installed, it'll automatically fall back to the core JSON::PP.

BEGIN { # look for JSON::XS, and if not available, fall # back to JSON::PP to avoid requiring non-core modules my $json_ok = eval { require JSON::XS; JSON::XS->import; 1; }; if (! $json_ok){ require JSON::PP; JSON::PP->import; } }

Replies are listed 'Best First'.
Re^2: Can not use JSON module
by choroba (Cardinal) on Jun 02, 2017 at 14:00 UTC
    Isn't that what JSON does? See also JSON::MaybeXS .

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      But that's the point. The OP says that they cannot install JSON "due to limitation" (whatever that may mean). This is therefore a nice means for them to choose between the modules which actually do the work without having to install the non-core JSON module.

        JSON::XS is not in core, either, so what's the point in trying to use it?

        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      I thought so, but I'm sure when I tested it on a box without JSON::XS it failed. I could have been imagining things at the time after a long day of work, so I'll re-test this today.