4. In Perl…
sub first {
local $x = 1;
my $y = 1;
second();
}
sub second {
print "x=", $x, "n"; # Prints x=1.
print "y=", $y, "n"; # Doesn't.
}
first();
http://perl.plover.com/local.html
5. In Perl…
sub first {
local $x = 1;
my $y = 1;
second();
}
sub second {
print "x=", $x, "n"; # Prints x=1.
print "y=", $y, "n"; # Doesn't.
}
first();
http://perl.plover.com/local.html
I’m Erik Rose.\nI work at Mozilla.\nAnd I think programming should be fun.\n\nI had an unreasonable amount of fun writing stackful, an experiment in adding a new language feature in pure Python.\n\nBut first, a bit of a disclaimer…\n
Done basically to see if I could do it. As a bonus, useful.\nThe mission? Escape from global config in other ppl’s frameworks: a location of a DB, timeout behavior on a socket, a pathname for some file.\nI’ve found myself reaching over the course of years for a way to make exceptions to these assumptions of globalness.\nDynamic vars give us this ability.\n
For example (explain)\n\nYou might imagine how you can use this to shadow an undesired global for a limited time\n\nstackful lets you do this in Python…\n
(describe)\n\nAs you would hope, it is not only per stack frame but also per thread, so you can do this in web servers or other multithreaded programs.\n\nImplementation tricky. Main use case for stackful is other ppls libs which aren’t factored the way I want. So didn’t want attr access like threading.local, because it would break existing code. At this point, I was sad, because can’t intercept symbol reference.\n\nHowever, then it occurred to me that you don’t need to; it doesn’t have to manifest until you actually do something with it. So, it was a simple matter of overriding…\n
\n
(outer() calls middle, which calls inner()…)\n\n__add__, and inner() prints 10\n\nYou can even stack additional values on top of stackful variables.\n\nStackful is handy for things like Django settings, which are globals, or even for stdlib modules which store config values in module-level globals (socket lib timeout, I’m looking at you).\n
JI thub\n\nTest coverage is pretty decent, though I have a few more magic methods to test and implement.\n\nShould be able to use it. Let it be on your own head if you do. Most importantly, I had fun writing it, and I hope you had fun hearing about it.\n