in reply to When to use prototype inheritence?

One thing I don't like with classless objects is, that you can't easily temporarily patch/overwrite a method. In my tests I often do the following :

# (test for WWW::Mechanize::Shell) { my $called; local *WWW::Mechanize::get = sub { $called++; }; ok( $shell->foo() ); is( $called, 1, '$shell->agent->get was called one time"); };

This is relatively clean and dosen't require much mucking to find out the package of the sub I want to patch. A classless object would possibly have its subs in some package which is dynamically generated, which makes patching/overriding harder unless I fall back on Hook::LexWrap (and even then ...)

perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web

Replies are listed 'Best First'.
Re: Re: When to use prototype inheritence?
by bsb (Priest) on Sep 08, 2003 at 09:23 UTC
    It depends on the system, but I think you could.
    Admittedly not with local(*F::Q::name).

    You could probably just do:

    my $object = $proto->new(); $object->set(some=>'stuff'); { my $test_object = $object; $test_object->{method} = sub { print "got:".$_[0]->{_proto_}->method() }; $test_object->method(); }
    This would depend on how the classless/prototype system works. (I don't know one well enough to give a full example)
      I believe that with Class::Classless, you can locally override the specific hash value you're interested in:
      local $test_object->{method} = sub { print "got:".$_[0]->{_proto_}->method() };