title: How does Perl handle the shebang line?
body: <p>I'm trying to understand how <code>perl</code> deals with the shebang line.</p>
  
  <p>I <em>used</em> to think that any interpreter mentioned in the "command position" on the command line would take precedence over one mentioned in the shebang line.  For example, if an executable script called <code>demo</code> looks like this</p>
  
  <pre><code>#!/usr/local/bin/perl-5.00503
  
  printf "$]\n";
  </code></pre>
  
  <p>...then I would observe the following:</p>
  
  <pre><code>$ ./demo
  5.00503
  % /usr/local/bin/perl-5.22 ./demo
  5.022003
  </code></pre>
  
  <p>IOW, in the first execution, the interpreter in the shebang is the one running, while in the second it is the one mentioned on the command line.  So far so good.</p>
  
  <p>But now, if I change the "interpreter" on the shebang to something like <code>/usr/bin/wc</code>, then it always beats any <em><code>perl</code></em> interpreter I mention on the command line:</p>
  
  <pre><code>% cat demo-wc
  #!/usr/bin/wc
  
  printf "$]\n";
  
  % ./demo-wc                           # produces the expected behavior
         4       3      31 ./demo-wc
  % /usr/local/bin/perl-5.22 ./demo-wc
         4       3      31 ./demo-wc
  % /usr/local/bin/perl-5.14 ./demo-wc
         4       3      31 ./demo-wc
  </code></pre>
  
  <p>AFAICT, this special behavior seems to be limited <code>perl</code> interpreters; non-<code>perl</code> interpreters, such as <code>/bin/bash</code>, do "overrule" the shebang:</p>
  
  <pre><code>% /bin/bash ./demo-wc
  $]
  </code></pre>
  
  <hr>
  
  <p>The bottom line is that <code>perl</code> seems to have radically different policies for handling the shebang depending on the interpreter mentioned.</p>
  
  <hr>
  
  <ol>
  <li>How does <code>perl</code> determine which policy to follow?</li>
  <li>What exactly are the policies in either case?</li>
  </ol>