perlrun Fun! Remote Script Execution with -x

By | Perl

Last night I was perusing Perl’s online documentation site (perldoc.pl). I really enjoyed the experience, it is well written and a treat to read! While checking out perlrun I found a tidbit that caught my attention.

  • -x tells Perl that the program is embedded in a larger chunk of unrelated text, such as in a mail message. Leading garbage will be discarded until the first line that starts with #! and contains the string “perl”. Any meaningful switches on that line will be applied. The program must be terminated with __END__ if there is trailing garbage to be ignored; the program can process any or all of the trailing garbage via the __DATA__ filehandle if desired.

This gave me an idea! I can launch my code straight from this website with this command line parameter. After testing it I am happy to report it works! One caveat I noticed was with the strict pragma on you can’t add the ampersand sigil to functions.

Here are 2 ways I found to execute code from a remote source with the -x parameter.

NOTE: Careful if you execute the command line below as printed. It will recursively rename all the files in the folder you are in. (I am just using it as an example).

METHOD #1

This uses the bash construct <() which sets the output of a command to a temporary process (process substitution). This will create a temporary path which can be opened like a file.

perl -x <(curl -sSL 'https://geekalicious.blog/recursive-web-friendly-file-renamer-with-perl/')

METHOD #2

This version pipes the HTML output to perl.

curl -sSL https://geekalicious.blog/recursive-web-friendly-file-renamer-with-perl/ | perl -x

Leave a Reply

Your email address will not be published. Required fields are marked *