How can I convert Perl one-liners into complete scripts?

前端 未结 6 813
旧巷少年郎
旧巷少年郎 2020-12-24 03:06

I find a lot of Perl one-liners online. Sometimes I want to convert these one-liners into a script, because otherwise I\'ll forget the syntax of the one-liner.

For e

6条回答
  •  情书的邮戳
    2020-12-24 04:02

    There are some good answers here if you want to keep the one-liner-turned-script around and possibly even expand upon it, but the simplest thing that could possibly work is just:

    #!/usr/bin/perl -p
    s/(\d+)/localtime($1)/e
    

    Perl will recognize parameters on the hashbang line of the script, so instead of writing out the loop in full, you can just continue to do the implicit loop with -p.

    But writing the loop explicitly and using -w and "use strict;" are good if plan to use it as a starting point for writing a longer script.

提交回复
热议问题