|Perl Monk, Perl Meditation|
Re: Clipboard transform keysby halley (Prior)
|on Aug 06, 2003 at 16:42 UTC||Need Help??|
Nice polyglot. It's extra-defensive. In the way of explanation, since not all Windows command scripts are so complicated,
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.