From: Robert Klemme Date: 2004-02-02T02:29:53+09:00 Subject: Re: check whether a class is defined, listing all classes "David Garamond" schrieb im Newsbeitrag news:401D1FF4.6000900@zara.6.isreserved.com... > ts wrote: > >>>>>>"D" == David Garamond writes: > > D> Hm, I should've added that I didn't want to involve 'eval'. But then why > > D> not, I'll just need to check the input using a simple regexp. > > > > well, if you think that a simple regexp can do anything against the evil > > eval, then you have perhaps a P background :-) > > Don't you also have a P background? :) (Or Matz? Or many others?) > > http://www.google.com/search?num=100&q=guy+decoux+perl > > Anyway, I didn't say regexp can make eval safe generally, but only for > my case: > > def class_defined?(classname) > classname.kind_of? String or > raise ArgumentError, "please give me string" > classname =~ /\A[A-Z][A-Za-z0-9_]*(::[A-Z][A-Za-z0-9_]*)*\z/ or > raise ArgumentError, "invalid class name" > eval("defined? " + classname) != nil You don't need to compare with nil, that's converting something to a boolean that is essentially a boolean already - unless, of course, if you want to hide the outcome of "defined?"... To make the methods semantic match the name you should do eval("defined? " + classname) && Class === eval(classname) Because otherwise you will get true for all sorts of constants. (Try with "IO::CREAT") > end > > Please do tell me if the above is unsafe... Looks safe enough. robert