kappa has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way to somehow assign to $self from inside a method so that I could recreate the object I'm using from scratch? Seems unlikely, but I'm currently looking into WWW::Automate guts (yeah, I'm still sick with automatic acceptance testing for my webapps) and see this:
sub pop_page_stack { my $self = shift; if (@{$self->{page_stack}}) { $self = pop @{$self->{page_stack}}; bless $self; } return 1; }

This method is intended to be used as: $agent->pop_page_stack; to simulate the "Go back" button in my browser. From my attempts at actually using WWW::Automate, I can say that this method fails. Looks like I found a bug, but how can the intended interface be implemented at all?

I'm now using my own workaround which (above all) changes the interface to $agent = $agent->pop_page_stack;.

Replies are listed 'Best First'.
Re: Recreating an object from its own method
by chromatic (Archbishop) on Jun 11, 2002 at 17:31 UTC
    Don't shift, then assign to $_[0].
Re: Recreating an object from its own method
by Joost (Canon) on Jun 12, 2002 at 12:57 UTC
    As mentioned above, you could assign to $_[0], but you can still get into trouble with this if your object reference is copied anywhere - then you would get 2 different objects, and the code you are using might get confused somewhere.

    For this reason, and as a matter of clarity, I think I would actually prefer my $agent = $agent->previous if you really need to create a new object.

    I do however wonder if in this case it would not be more efficient and clearer if you could just update the state of the object without creating a new one...

    -- Joost downtime n. The period during which a system is error-free and immune from user input.