Cpanel-JSON-XS icon indicating copy to clipboard operation
Cpanel-JSON-XS copied to clipboard

JSON::XS->new->new fails badly

Open cpansprout opened this issue 7 years ago • 3 comments

I know this is my fault, by I accidentally wrote code that did ->new on an object, and gave me a confusing error message:

Can't locate object method "encode" via package "JSON::XS=SCALAR(0x7fccca0070d0)" at -e line 1.

It would be helpful if JSON::XS->new could make sure that the class is not an object, just as pp_bless does.

Feel free simply to close this ticket if you do not think it is worth the extra check.

cpansprout avatar Jul 29 '17 02:07 cpansprout

I'm not sure.

Both JSON::XS new and Cpanel::JSON::XS new accept any scalar and use the stash of it as classname. Objects are allowed.

$o=Cpanel::JSON::XS->new;print ref $o->new returns an object of Cpanel::JSON::XS. $o=Cpanel::JSON::XS::new("JSON::XS");print ref $o->new returns an object of JSON::XS but with the fields of Cpanel::JSON::XS. It does no harm, it just uses the class of the existing object.

What pp_bless additionally does, but (Cpanel::)?JSON::XS not, are three checks:

  • "Attempt to bless into a freed package"
  • "Attempt to bless into a reference" I think you want this
  • "Explicit blessing to '' (assuming package main)"

That would be overkill to add those checks also. No other XS constructor does this, just PP constructors.

rurban avatar Jul 29 '17 15:07 rurban

Feel free to close it then, if you want.

cpansprout avatar Jul 30 '17 02:07 cpansprout

Still thinking a bit over this...

rurban avatar Jul 30 '17 05:07 rurban