Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^3: OO-Perl Question: How Do I get a List of derived Classes for a Base Class?

by BrowserUk (Patriarch)
on Mar 14, 2010 at 03:57 UTC ( [id://828520]=note: print w/replies, xml ) Need Help??


in reply to Re^2: OO-Perl Question: How Do I get a List of derived Classes for a Base Class?
in thread OO-Perl Question: How Do I get a List of derived Classes for a Base Class?

Choosing this type of implementation, it will be easy to extend the solution by a new commenttype: Only a new derived class is needed.

You choose a derived class to instantiate, according to a comment in the data. That's fair enough, but it doesn't explain why you need to build a list of all possible derivable types? There is presumably a relationship between the value of the comment, and the name of the derived class to instantiate?

All that knowing what is available achieves, is the ability to take a slightly earlier exit if you read a comment that requires a class that isn't available. But the additional complexity required to decide not to try and instantiate an instance of an unavailable class doesn't justify itself when simply making the attempt to instantiate the instance tells you the same information for free.

Eg.

my %derived = listOfDerivedClasses( 'Element' ); while( <SQL> ) { my( $type ) = m[...]; die "Derived type $type not available" unless exists $derived{ $ty +pe }; push @instances, $type->new(); }

Versus:

while( <SQL> ) { my( $type ) = m[...]; my $inst; eval{ $inst = $type->new(); 1 }; die "Derived type $type not available:'$@'" if $@; push @instances, $inst; }

I guess my question is, does the extra complexity of providing introspection buy you something that is otherwise unavailable; or provide for it in some way that is sufficiently better to justify the cost?


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^4: OO-Perl Question: How Do I get a List of derived Classes for a Base Class?
by CountZero (Bishop) on Mar 14, 2010 at 19:17 UTC
    All that knowing what is available achieves, is the ability to take a slightly earlier exit
    I think there is more than that to it.

    How would the main loop know how to parse the SQL for "new" types of comments? Probably (really just a guess) the sub-classes provide a method that returns a regex which can be used to parse the SQL and check for that specific type of comment. Adding another sub-class and going through the list of sub-classes would make that method available without having to hard-code it into the main loop.

    Of course that still leaves you the task to use or require all the sub-classes in some automated way.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      How would the main loop know how to parse the SQL for "new" types of comments?

      It's not that hard to see a comment format/regex combination that allows for that.

      A regex of m[-- class:(\w+)] for instance would pick out a class name that can be combined with some root to require and instantiate the appropriate subclass if available.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2024-03-29 09:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found