Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Nice polyglot. It's extra-defensive. In the way of explanation, since not all Windows command scripts are so complicated,
@rem = '--*-Perl-*-- @echo off perl -x -S "%~dpnx0" %1 %2 %3 %4 %5 %6 %7 %8 %9 goto endofperl @rem '; #!perl #line 8 YOUR PERL CODE HERE; __END__ :endofperl
This can be run as a Windows command file itself, or as a Perl script directly, and the syntax hacks are designed to fool both interpreters into ignoring the parts intended for the other.

In perl, lines 1..5 are @rem = '...'; and the resulting array is otherwise ignored. In Windows script, that's five separate commands: (1) an unechoed remark @rem, (2) an unechoed command to turn off further automatic echoes @echo off, (3) an invocation of perl on the script itself with its first nine arguments, (4) a command to skip any other lines until one matching /^:endofperl/, and another unechoed remark @rem. The first line, as a bonus, suggests the file should be edited in perl-mode for some smart editors.

The perl -x tells perl to skip anything ahead of the shebang line matching /^\#\!/. This is only seen when the command script invokes Perl. It shouldn't be necessary since the @rem=''; trick should armor these lines, but it's defensive. The odd "%~dpnx0" reconstructs the %0 variable explicitly with all possible components: drive, path, name, extension.

The #line 8 trick aligns perl's error-reporting features with the actual polyglot script's lines. Otherwise, any errors would be reported, thanks to the perl -x as numbered relative to the shebang line.

Of course, perl quits reading at the __END__ token, so anything following is ignored by perl, but the last line is the aforementioned target for the command script's goto endofperl command.

[ e d @ h a l l e y . c c ]

In reply to Re: Clipboard transform keys by halley
in thread Clipboard transform keys by PhilHibbs

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (4)
    As of 2020-09-24 17:55 GMT
    Find Nodes?
      Voting Booth?
      If at first I donít succeed, I Ö

      Results (135 votes). Check out past polls.