[perl-Glib-Object-Introspection] Fetch the value of constants only once



commit 2b04a717a12f9b2f797e71ef84a475303ceb0273
Author: Torsten Schönfeld <kaffeetisch gmx de>
Date:   Sat Nov 13 01:38:56 2010 +0100

    Fetch the value of constants only once

 lib/Glib/Object/Introspection.pm |   13 ++++++++++---
 1 files changed, 10 insertions(+), 3 deletions(-)
---
diff --git a/lib/Glib/Object/Introspection.pm b/lib/Glib/Object/Introspection.pm
index 5bb2a2c..53715dd 100644
--- a/lib/Glib/Object/Introspection.pm
+++ b/lib/Glib/Object/Introspection.pm
@@ -45,7 +45,8 @@ sub setup {
   my ($functions, $constants) =
     __PACKAGE__->_register_types($basename, $package);
 
-  no strict 'refs';
+  no strict qw(refs);
+  no warnings qw(redefine prototype);
 
   foreach my $namespace (keys %{$functions}) {
     my $is_namespaced = $namespace ne "";
@@ -71,8 +72,14 @@ sub setup {
     my $corrected_name = exists $name_corrections->{$auto_name}
       ? $name_corrections->{$auto_name}
       : $auto_name;
-    *{$corrected_name} = sub {
-      __PACKAGE__->_fetch_constant($basename, $name);
+    # Install a sub which, on the first invocation, calls _fetch_constant and
+    # then overrides itself with a constant sub returning that value.
+    *{$corrected_name} = sub () {
+      my $value = __PACKAGE__->_fetch_constant($basename, $name);
+      {
+        *{$corrected_name} = sub () { $value };
+      }
+      return $value;
     };
   }
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]