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

Re: checking a method is not subclassed

by water (Deacon)
on Feb 02, 2006 at 17:04 UTC ( #527377=note: print w/ replies, xml ) Need Help??


in reply to checking a method is not subclassed

I'm missing something here, or I wasn't clear. Here's some code:

# untested package Foo; sub bleep {...} sub baz { ... } package NewFoo; use base qw(Foo); sub baz { ... } package main; my $a = Foo->new; my $b= NewFoo->new; $a->bleep; $b->bleep; $a->baz; $b->baz;
In this example, I am cool with a user calling bleep on either 'a' or 'b'. I'm also cool with a user calling baz on either 'a' or 'b'. What I'd like to detect from Foo is that a subclass (NewFoo) overrode a certain method. That is, $b->baz calls NewFoo::Baz, as that overrode Foo::Baz.

I'm looking for a way for Foo to detect and carp that a subclass took away its rights to own the code for method baz.

Probably this isn't "nice" OO -- "thou shalt never subclass me, dammnit" isnt friendly -- but I'm wondering how to do it anyway.

I really like the suggestion about just saying "never subclass baz" in the docs (++ to that post), but need something stronger.

The comments above about checking the ref on self don't seem relevant to me, as here the problem is that Foo::baz never gets involved when someone calls baz on $b... it is too late, the baz is then a NewFoo baz at that point.....?

water


Comment on Re: checking a method is not subclassed
Download Code
Re^2: checking a method is not subclassed
by salva (Monsignor) on Feb 02, 2006 at 17:46 UTC
    if you really want to do it, you can use a CHECK block to browse all the symbol tables once all the modules and the script have been loaded and parsed, examining with packages subclass your special classes and that they don't redefine the methods you want to protect.

    You can even use a subrutine attribute to do it (as :final).

    Though modules could still work around that defining new methods at runtime via eval or just playing with the symbol tables.

      This sounds interesting. Yeah, I know others can use eval and play games with the symbol table at runtime. Leaving out those may-I-call-them pathological cases, could you say more about your approach?
Re^2: checking a method is not subclassed
by gam3 (Curate) on Feb 02, 2006 at 18:28 UTC
    You are not clear where or when the carp should happen.
    The code that tye and I gave you would die on the my $b=NewFoo->new;.

    Do you want a function like Foo::overriden('foo') that would return the name of classes that overrode method foo()?

    -- gam3
    A picture is worth a thousand words, but takes 200K.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://527377]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2014-09-23 13:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (221 votes), past polls