Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

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

by BluePerl (Novice)
on Mar 14, 2010 at 01:09 UTC ( #828499=note: print w/ replies, xml ) Need Help??


in reply to Re: 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?

I wrote a simple parser, which looks for comments inside SQL-Code. If it find a specific remark, the parser store data and generates some output for documentation. There are different types of comments which generates different type of documentation parts. The main idea is to generate a super class with the main methods an derive some class as an implementation for a specific comment type.

This is no problem until here. In the main loop parse the sql-code and had to look, which derived object matches. The parser should try to match each derived object without write down each typename.

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


Comment on Re^2: OO-Perl Question: How Do I get a List of derived Classes for a Base Class?
Re^3: OO-Perl Question: How Do I get a List of derived Classes for a Base Class?
by BrowserUk (Pope) on Mar 14, 2010 at 03:57 UTC
    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.
      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
Node Status?
node history
Node Type: note [id://828499]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2014-09-20 00:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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











    Results (151 votes), past polls