Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: Saving HTTP::Cookies into Netscape format using bless/re-bless

by tobyink (Canon)
on May 18, 2021 at 19:12 UTC ( [id://11132716] : note . print w/replies, xml ) Need Help??

in reply to Saving HTTP::Cookies into Netscape format using bless/re-bless

Assuming $jar is your original cookie jar, you can just do:

require HTTP::Cookies::Netscape; $jar->HTTP::Cookies::Netscape::save("mycookies.txt");

No need to rebless. It could cause problems if save calls helper methods on $self which aren't defined in the parent class. But reblessing also could cause problems, so shruggles.

Replies are listed 'Best First'.
Re^2: Saving HTTP::Cookies into Netscape format using bless/re-bless
by bliako (Monsignor) on May 19, 2021 at 07:30 UTC

    Hmmm, you are doing this: HTTP::Cookies::Netscape::save($jar, "mycookies.txt"); which as you say has the problem if internally save() calls any of the "overwritten" methods (quoted because they are not overwritten at all). In this case these are just save() and load(). And I see one more special-but-common case: if it recurses save(). Also calling a logger helper method would be printing parent class signature.

    I am reblessing from a parent class and not just any class, so all state variables and internal methods that may be needed by the child will be inherited. That minimizes the risks. But are there still any?

    I see one, if subclass has its own state variables which are set/changed during operations prior to save() for example if it implements its own set_cookie() or new(). That bug will hurt! But it will not affect subclass-specific constants and such, these will assume their initial value upon reblessing.

    The use-case I had in mind is to use this "method" in order to transform from one cookie format to another using all those subclasses under HTTP::Cookies but I will have to examine each of these for above problems. I am not sure if other OO-based languages avoid this problem. I don't think Java does that or indeed can do that. It's up to the programmer to adhere to the "princaiples" (sic).

    bw, bliako

      Actually, these behave differently:

      Some::Class::method( $obj, @args ); $obj->Some::Class::method( @args );

      If Some::Class doesn't contain a sub called method, the former will fail, but the latter will walk Some::Class's @ISA.

        thanks++, I did not know that.