in reply to Re: Re: Why does Perl use dynamic scoping?
in thread Why does Perl use dynamic scoping?

demerphq~

If I recall correctly from my programming language class, what you are doing there is actually called fluid scoping.

In a language without side effects, fluid and dynamic scoping are the same. However in a language with side effects, dynamic scope has the effect of treating all variables as globals. Thus fluid scoping with side effects into what is more commonly thought of as dynamic scoping in a language without them.

Boots
---
Computer science is merely the post-Turing decline of formal systems theory.
--???
  • Comment on Re: Re: Re: Why does Perl use dynamic scoping?

Replies are listed 'Best First'.
Re: Re: Re: Re: Why does Perl use dynamic scoping?
by demerphq (Chancellor) on Feb 07, 2003 at 22:38 UTC
    I am at loss here. A google for "fluid scoping" finds only two matches, neither of which are on line right now. Can you explain what you mean?

    However in a language with side effects, dynamic scope has the effect of treating all variables as globals.

    First off, I dont grok "a language with side effects". I understand a "function with side effects" or "a function with no side effects" but I dont quite see how that can be applied to a language. And when I do, it seems logical to me to posit that "a language without side effects" doesn't do anything. Can you explain this idea to me?

    Why should dynamic scoping mandate only global variables? Presumably Perl is a language with side effects so how does that square with the observation that we have dynamic scoping of elements of lexical composite variables like hashes and arrays? Or is that still fluid scoping?

    Im interested to hear more about this.

    :-)

    Incidentally my understanding is that dynamic scoping was introduced to perl to aleviate the problems of only having globals. Not that some variables had to be global so that they could be dynamically scoped.

    --- demerphq
    my friends call me, usually because I'm late....

Re^4: Why does Perl use dynamic scoping?
by adrianh (Chancellor) on Feb 07, 2003 at 22:23 UTC

    I've only ever come across "fluid scoping" as a synonym for "dynamic scoping". Can you provide some refs and a bit more detail? My poor brain not understanding the distinction you are trying to draw:-)

      All~

      I can try, but most of what I say is just from memory of the Programming Languages class that I took... Thus I cannot guarantee perfect recall...

      The place where we learned about this is in closures and functional languages. In a purely functional language one cannot assign to variables (as that would be a side-effect). So the question becomes how is binding done, lexically or dynamically. In lexical binding you look for the most closely nested variable of that name in the code. In dynamic binding you look for the most closely defined variable in the call stack. Thus one code snippet can produce to different values depending on the binding scheme:
      bar(z) { return x+z; } baz(x) { return bar(2); }
      In such code, bar will access the x from baz, if the language has dynamic scoping.

      I was told that the way to think of this in a language with side-effects is that all variables can be thought of as global, since they all access the most recently referenced version. So what we have here is fluid scoping which causes changes to a variable not to propogate to the outside world, by causing them to revert at end of scope. This would happen automatically in a language without side-effects, because you cannot assign to a variable...

      That is at least the way I understand it. I could be mistaken.

      Boots
      ---
      Computer science is merely the post-Turing decline of formal systems theory.
      --???