<p><a href="https://en.wikipedia.org/wiki/Scope_%28computer_science%29">https://en.wikipedia.org/wiki/Scope_%28computer_science%29</a></p>
<p>In <a href="https://hackerpublicradio.org/eps.php?id=2739">hpr2739</a>, Dave talked briefly about local variables. But what are they?</p>
<p>In most modern languages, especially in compiled languages, &quot;local&quot; means that the value of a variable cannot be directly known, by looking up the name, outside the bounds of that function, but that’s not how it works in bash.</p>
<p>Languages like C and Python have <em>lexical scope</em>. Lexical scope means local variables are local in the <em>text</em>. The names are local.</p>
<p>If I’m writing code that is textually located outside the function, I cannot even describe how to access the variables within the function, because <code>myvariable</code> in my function is not the same variable, not the same place, as <code>myvariable</code> in your function.</p>
<p>Languages like Bash and Elisp have <em>dynamic scope</em>. That means local variables are local in <em>time</em>. The names are global.</p>
<p>What happens when you declare a variable <code>local</code> in bash is that the existing value of that variable is stowed away, to be brought back when your function exits.</p>
<pre><code>#!/usr/bin/env bash
function sayscope() {
    echo The scope is $whatsmyscope
}

function globalscope() {
    whatsmyscope=global
}

function dynamicscope() {
    whatsmyscope=dynamic
}

function localscope() {
    local whatsmyscope=local
    sayscope
    dynamicscope
    sayscope
}

globalscope
sayscope
localscope
sayscope</code></pre>
<pre><code>The scope is global
The scope is local
The scope is dynamic
The scope is global</code></pre>
<p>Perl has both, and it calls them <code>local</code> (dynamic scope, like bash) and <code>my</code> (lexical scope):</p>
<pre><code>#!/usr/bin/env perl
use v5.10;

sub sayscope {
    say &quot;Dynamic scope is $whatsmyscope&quot;;
}

sub globalscope {
    $whatsmyscope=&quot;global&quot;;
}

sub dynamicscope {
    $whatsmyscope=&quot;dynamic&quot;;
}

sub lexicalscope {
    my $whatsmyscope=&quot;lexical&quot;;
    say &quot;Lexical scope is $whatsmyscope&quot;;
    sayscope;
}

sub localscope {
    local $whatsmyscope=&quot;local&quot;;
    sayscope;
    dynamicscope;
    sayscope;
    lexicalscope;
}

globalscope;
sayscope;
localscope;
sayscope;</code></pre>
<pre><code>Dynamic scope is global
Dynamic scope is local
Dynamic scope is dynamic
Lexical scope is lexical
Dynamic scope is dynamic
Dynamic scope is global</code></pre>
<p>You almost never want to use <code>local</code> in Perl, it’s mostly there for historical reasons — lexical scope is a Perl 5 feature. <a href="https://perl.plover.com/local.html" class="uri">https://perl.plover.com/local.html</a> covers well the remaining few and narrow exceptions where <code>local</code> might be useful.</p>
<p>As dynamic scope has <em>some</em> valid use, it’s available in some otherwise lexically scoped languages. For example, Common LISP has the <em>special</em> form, and several Schemes and Racket have <em>parameter objects</em>:</p>
<ul>
<li><p><a href="https://www.lispworks.com/documentation/HyperSpec/Body/d_specia.htm" class="uri">https://www.lispworks.com/documentation/HyperSpec/Body/d_specia.htm</a></p></li>
<li><p><a href="https://srfi.schemers.org/srfi-39/srfi-39.html" class="uri">https://srfi.schemers.org/srfi-39/srfi-39.html</a></p></li>
<li><p><a href="https://docs.racket-lang.org/reference/parameters.html" class="uri">https://docs.racket-lang.org/reference/parameters.html</a></p></li>
</ul>
<p>To dig fully into the history and flora of dynamic and lexical scope merits another episode.</p>


