Hi, basically the first three lines are executed/interpreted by 2-3 programs (at least under *nix): the shell, the perl executable, and Emacs.
The first line tells Emacs to switch into (c)perl-mode. It is ignored by the shell, the kernel, and the perl executable (#, not #!...).

The second line is interpreted by the shell first. It basically re-evaluates the call to your shell(!) script when called for the first time. Then the shell executes the evaluated string (e.g. perl -S yourscript.pl arg1 arg2 arg3) which is an explicit call to the perl executable. Here, the first call of the script stops because the perl executable (e.g. /usr/bin/perl) replaces the current shell (exec), so the shell never sees the third line and thus, it cannot complain about the syntactically incorrect third line.

Now, the same script is executed by the perl executable. The -S switch tells perl to search your environments $PATH variable for directories where it may find your script. Perl ignores the first comment and interprets the 2nd and 3rd line as a conditional statement which condition is always false. So, it does not execute it and goes happily along its way...as a Perl script/program.

Why? Because the call to perl (the executable) does not use an absolute path, it is called with respect to your shells current $PATH environment. So you can avoid the shebang-line like #!/usr/bin/perl which uses an absolute path. Furthermore, since the first line does not start with #! the script is might be executed with your current shell (environment). See cdarke's comment for further details.
You might be interested in looking up the man pages for env.

Update: Some clarifications added (s/perl program/perl executable/g, meaning e.g. /usr/bin/perl). List of programs added.
This response belongs to a SoPW-question that originally looked like:

# use perl -*-Perl-*- eval 'exec perl -S $0 ${1+"$@"}' if 0;


In reply to Re: Understanding code. by Perlbotics
in thread Understanding code. by sashac88

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.