in reply to How to a construct objects with variables as class names?

What you are trying to do is similar in principle to variable variables, though not nearly as insecure. Still, who knows what badness may result from instantiating an object into a subclass that you're not expecting. For that reason, you may want to create a hash of accepted widget subclasses, and modify your instantiation like so:
my $widget = $fullClass->new($widgetID) if exists $OK_CLASSES{$fullC +lass};
   MeowChow                                   
               s aamecha.s a..a\u$&owag.print

Replies are listed 'Best First'.
Re: Re: How to a construct objects with variables as class names?
by joecamel (Hermit) on Feb 14, 2001 at 12:28 UTC
    Good suggestion, though I'm actually checking for that in the sub that gets the classname from the database. If the query returns nothing, it errors out. Defining the names in the database is at least as reliable as putting them in an array, I think. Since I use all of the widgets at the top, that should catch any potential mistakes in the db.

    I'm curious to know if there is a way to get around useing them though -- I would ideally like to be able to add widgets by simply:

    1) add the class module to the directory
    2) add the class name to the database

    The above sub resides in a module running on mod_perl, so every time I change it, I have to stop and restart the http server, which I'd rather not have to do. Of course, I realize there are some stability problems that could arise when doing this sort of thing.

    joecamel

      Sure, this is secure so long as the table you're checking the values against is comprised of data that only you (not your users) can manipulate, and it sounds like this is what you're doing.

      Regarding your second question, you may want to have a look at Related question: loading modules at run time. You will also want to do something like:

      eval "require $pkg";
      because of the differences in the way require EXPR works from require BAREWORD.

         MeowChow                                   
                     s aamecha.s a..a\u$&owag.print
        Awesome, I eliminated the uses from my module, and added

        eval "require $fullClass";

        to the subroutine -- it worked perfectly.

        Thanks, MeowChow and Adam, for your help.