Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Exploring context and globals with Nu

From Tim in mailing list:

Global name definitions are put into the symbol table; that is
slightly more efficient than normal assignments (such as those made
with the set, macro, and function operators), which are kept in
NSDictionaries. It also makes them easier to use in Nu code that is
called from Objective-C without specifying a context, which you can do
by sending the evalWithContext: message to a parsed code object with a
nil argument. Usually that happens in Objective-C, but since you can
send nearly any ObjC message from Nu, here's a little demo in nush
[ed: I expanded it to show more information]:

$ nush
Nu Shell.
% (parse "(puts 22)")
(progn (puts 22))
% (set prog (parse "(puts 22)"))
(progn (puts 22))
% (prog class)
NuCell
% (set progx (parse "(puts x)"))
(progn (puts x))
% (progx class)
NuCell
% (set x 23)
23
% (x class)
NSCFNumber
% (eval progx)
23
()
% (prog evalWithContext: nil)
22
()
% (progx evalWithContext: nil)
NuUndefinedSymbol: undefined symbol: x
% (progx evalWithContext: (context))
23
()
% (global x 50)
50
% (progx evalWithContext: (context))
23
()
% (progx evalWithContext: nil)
50
()

Emulate register_globals off

function unregister_GLOBALS()
{
if (!ini_get('register_globals')) {
       return;
   }

   // Might want to change this perhaps to a nicer error
   if (isset($_REQUEST['GLOBALS']) || isset($_FILES['GLOBALS'])) {
       die('GLOBALS overwrite attempt detected');
   }

   // Variables that shouldn't be unset
   $noUnset = array('GLOBALS',  '_GET',
                     '_POST',    '_COOKIE',
                     '_REQUEST', '_SERVER',
                     '_ENV',    '_FILES');

   $input = array_merge($_GET,    $_POST,
                         $_COOKIE, $_SERVER,
                         $_ENV,    $_FILES,
                         isset($_SESSION) && is_array($_SESSION) ? $_SESSION : array());
  
   foreach ($input as $k => $v) {
       if (!in_array($k, $noUnset) && isset($GLOBALS[$k])) {
           unset($GLOBALS[$k]);
       }
   }
}

unregister_GLOBALS();


Source: http://www.zend.com/manual/faq.misc.php#faq.misc.registerglobals
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS