{"id":209,"date":"2007-10-01T10:30:40","date_gmt":"2007-10-01T09:30:40","guid":{"rendered":"https:\/\/www.tomfotherby.com\/blog\/index.php\/2007\/10\/perl-pie\/"},"modified":"2010-09-14T11:48:37","modified_gmt":"2010-09-14T10:48:37","slug":"perl-pie","status":"publish","type":"post","link":"https:\/\/www.tomfotherby.com\/blog\/index.php\/2007\/10\/perl-pie\/","title":{"rendered":"Perl Pie"},"content":{"rendered":"<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.tomfotherby.com\/Images\/pearl.jpg?w=620&#038;ssl=1\" alt=\"Holy Pearl\" class=\"alignleft size-full\" \/> <em>&#8220;How do I change &#8220;pelican&#8221; to &#8220;pecan&#8221; in all the files in a directory?&#8221;. &#8220;Dude, use Perl Pie&#8221;!<\/em><\/p>\n<p>I&#8217;m pretty familiar with Perl, having used it on and off whenever there&#8217;s a string manipulation related job to be done. I like Perl despite all it&#8217;s faults. I like writing Perl (not so keen on reading it though \ud83d\ude42 ). I&#8217;ve been using Perl one-liners where I probably should be using Shell scripts but I used to forget what the right flags are until I heard the term &#8220;Perl Pie&#8221;.<\/p>\n<pre>perl -p -i -e 's\/before\/after\/g' your.files*<\/pre>\n<p>-e is for &#8220;execute&#8221; &#8211; It allows you to define Perl code to be executed by the compiler<\/p>\n<p>-p is for &#8220;process&#8221; &#8211; It adds a loop around your -e code so that it is applied to each line of any specified files and the contents of $_ are printed out and errors are thrown if a file can&#8217;t be read. You can think of it like it adds this:<\/p>\n<pre>while (<>) {\r\n  # your -e code goes here\r\n} continue {\r\n  print or die \"Can't open blah: $!\\n\";\r\n}<\/pre>\n<p>-i is for &#8220;In-place Editing&#8221; &#8211; Without it, no files will be changed. With it, Perl renames the input file and reads from this renamed version while writing to a new file with the original name. If -i is given a string argument, then that string is appended to the name of the original version of the file. for example, Sometimes it&#8217;s handy to use -i~ so Perl creates a backup file before making your changes. if -i has no arguments, the file names don&#8217;t change.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;How do I change &#8220;pelican&#8221; to &#8220;pecan&#8221; in all the files in a directory?&#8221;. &#8220;Dude, use Perl Pie&#8221;! I&#8217;m pretty familiar with Perl, having used it on and off whenever there&#8217;s a string manipulation related job to be done. I like Perl despite all it&#8217;s faults. I like writing Perl (not so keen on reading [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-209","post","type-post","status-publish","format-standard","hentry","category-techjournal"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.tomfotherby.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tomfotherby.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tomfotherby.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tomfotherby.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tomfotherby.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=209"}],"version-history":[{"count":2,"href":"https:\/\/www.tomfotherby.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/209\/revisions"}],"predecessor-version":[{"id":1674,"href":"https:\/\/www.tomfotherby.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/209\/revisions\/1674"}],"wp:attachment":[{"href":"https:\/\/www.tomfotherby.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tomfotherby.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tomfotherby.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}