-----BEGIN PGP SIGNED MESSAGE----- Moin,
On Tuesday 24 February 2004 23:41, chromatic wrote: > On Tue, 2004-02-24 at 11:02, John Beppu wrote: > > The has() method in this release has a bug. > > > > Here's how it looks right now: > > > > sub has > > { > > my ($class, $define) = @_; > > scalar grep { exists $$sdl_config{$_}{$define} } keys > > %$sdl_config; } > > > > Unfortunately, even if certain attributes are set to 0, > > the existence check still passes. For example, I don't > > have SDL_ttf on my system, and the $sdl_config hashref > > knows this. However, > > > > SDL::Config->has('SDL_ttf') > > > > returns 1; > > > > Get rid of "exists" and it'll work as advertised. > > True, but it'll eat up memory in the hash if you pass undefined > subsystem names. That's why the original version was: > > sub has > { > my ($class, $define) = @_; > > while (my ($name, $subsystems) = each %$sdl_config) > { > next unless exists $subsystems->{ $define }; > return $subsystems->{ $define } ? 1 : 0; > } > > return; > } > > Maybe it was silly to optimize this never to use any more memory or to > keep an O(n) operation O(n/2) or better, but it's not susceptible to > this bug. However, exist is not always neccessary (I formerly thought it is): # perl -MData::Dumper -le 'my $hash = { u => x }; print "foo" if ! $hash->{a}; print Dumper($hash);' foo $VAR1 = { 'u' => 'x' }; See, $hash->{a} hasnt been created :) The code also has the problem that different subsystems might share the same key, and thus advertise that somethin is there even though it isn't. Why isnt sdl_config just a list (hash) of supported/unsupported things instead a nested one? E.g. $sdl-config->{foo}->{barfel} = 1; $sdl-config->{fub}->{barfel} = 0; What if you now test for has('barfel'); Which one will come first!? A flat structure would allow: sub has { my ($class, $define) = @_; return exists $sdl_config->{$define} ? $sdl_config->{$define} : undef; } which is much simpler. > On the other hand, I suspect the new version performs better in list > context. In addition, there are like 10 different values, and it doesnt matter _at all_ what you do with that hash in what order, since it is executed almost always only once, and would in the worst case create a dozend hash keys (if ever, see above). This is not a place where you should optimize code and thus introduce bugs in the code. :) Cheers, Tels - -- Signed on Wed Feb 25 18:57:07 2004 with key 0x93B84C15. Visit my photo gallery at http://bloodgate.com/photos/ PGP key on http://bloodgate.com/tels.asc or per email. "Retsina?" - "Ja, Papa?" - "Warp 3." - "Is gut, Papa." -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.2-rc1-SuSE (GNU/Linux) Comment: When cryptography is outlawed, bayl bhgynjf jvyy unir cevinpl. iQEVAwUBQDzkRXcLPEOTuEwVAQFOEQf+PW4RmxKtCH1fYoTgU4N86hpsTDfczQ22 EbGuzFuF/Wdl+zPZyLp3rAx7HX7puctqYILVrD7uHSK6MjkoesW8dRGjpuPEIMRj jvi+drHVflCgmiZJ5OwQBgh1RsWCVCSTf3nzcNAMpoheHxKeF/MqXd4N5KoFOoP2 wHJyb6+47bTYSTUjCtSnMW5pCtHCGGE6hug5L1/T/qF3BaVDkUKXOC9KPWZIvf/9 huPZLLBl3RBBv9Ke2oPU49J0Q98uum1AyrzDzXqFXpBdBEkZuKEXAs9Lr7qJDhWq Sqbv5lFWHiD7McEuPR7yNew+qx9yP5mR23T7KVIplUB45LPmsJvIeQ== =cYuM -----END PGP SIGNATURE-----