incorrect methods yield success
Sorry, that's not really much to go on, the behavior looks correct to me. $jsonpackage->new creates a new object (in this case of type Cpanel::JSON::XS). encode is a method, but encode_json is not, but in the second call of jsonsolute, $json->$jsonefunction calls encode_json as a method, meaning that the parameters that the function encode_json sees are actually ($json, {key => 'value'}), and it doesn't know how to handle the object $json, which is of type Cpanel::JSON::XS, so it dies.
use warnings;
use strict;
use Data::Dump;
dd jsonsolute('JSON::MaybeXS', 'encode', 'decode');
dd jsonsolute();
print "Done\n";
sub jsonsolute {
my ($jsonpackage, $jsonefunction, $jsondfunction) = @_;
$jsonpackage ||= 'JSON::MaybeXS';
$jsonefunction ||= 'encode_json';
$jsondfunction ||= 'decode_json';
dd $jsonpackage, $jsonefunction, $jsondfunction;
if ( !defined eval "require $jsonpackage; 1" ) {
die "package $jsonpackage missing\n";
} else {
my $json = $jsonpackage->new(pretty => 1, canonical => 1);
dd $json;
my $encoded = $json->$jsonefunction({key => 'value'});
my $decoded = $json->$jsondfunction('{"key":"value"}');
dd $encoded, $decoded;
return $encoded, $decoded;
}
}
__END__
("JSON::MaybeXS", "encode", "decode")
bless(do{\(my $o = "")}, "Cpanel::JSON::XS")
("{\n \"key\" : \"value\"\n}\n", { key => "value" })
("{\n \"key\" : \"value\"\n}\n", { key => "value" })
("JSON::MaybeXS", "encode_json", "decode_json")
bless(do{\(my $o = "")}, "Cpanel::JSON::XS")
encountered type (HASH(0x52aeb6d595a8), 0x80801) was specified for 'Cp
+anel::JSON::XS=SCALAR(0x52aeb6e99850)' at 11118555.pl line 20.