in reply to Why does Perl use dynamic scoping?
Also, is dynamic scoping still hanging around just for posterity? Is Perl moving away from dynamic scoping?
"No" and "No" in my opinion :-)
I don't need dynamic scoping often, but when I do I want it there ready and waiting.
I think many of the perceived problems with dynamic scope and perl are due to the fact that it was around before lexical variables - so people had to be re-educated to use lexicals where appropriate.
Removing dynamic scope because some people misuse it where lexicals would be more appropriate would be like removing map because some people misuse it in a void context instead of foreach :-)
Other languages allow you to do even more with dynamic scoping. For example Pop-11's dlocal statement allows you to dynamically scope arbritary expressions as well as variables. For example (taken from Pop-11 docs):
;;; make a three element list vars list =[1 2 3]; define test(); ;;; dynamically scope the second element dlocal %hd(tl(list))%; [original list ^list]=> ;;; update second element 5 -> hd(tl(list)); [updated list ^list] => enddefine; test(); ** [original list [1 2 3]] ** [updated list [1 5 3]] ;;; But the value of hd(tl(list)) has been restored on exit: list => ** [1 2 3]
(Yes, I know you can use tie in perl to bind functionality to dynamic scoping - but it's not simple).
In Pop-11 you can even dynamically scope lexicals if you want.
Personally I hope that perl6 will take dynamic scoping to new levels rather than taking it away.
Anyway - the point I'm trying to make is that dynamic scope can be a useful and powerful feature. Just because some people misuse it isn't a reason for it to be removed.
|
|---|