<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Virtual Clouds &#187; perl</title>
	<atom:link href="http://www.virtual-clouds.com/tag/perl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.virtual-clouds.com</link>
	<description>Erlang and Other Animals</description>
	<lastBuildDate>Mon, 19 Jul 2010 17:42:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>perl one liners = magic</title>
		<link>http://www.virtual-clouds.com/2009/10/81/magic-perl-one-liners/</link>
		<comments>http://www.virtual-clouds.com/2009/10/81/magic-perl-one-liners/#comments</comments>
		<pubDate>Sun, 04 Oct 2009 22:41:24 +0000</pubDate>
		<dc:creator>ofer affias</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[methodology]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://www.virtual-clouds.com/?p=81</guid>
		<description><![CDATA[a word about the power of unix shell commands: perl one liners, i.e. one line of perl that can save you hours of error-prone hand changes. when i want to do some massive changes in my code this is what i use. say you need replcae a record name from abc_record to xyz_record, first if <a href='http://www.virtual-clouds.com/2009/10/81/magic-perl-one-liners/'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.virtual-clouds.com/wp-content/uploads/2009/10/perl-150x150.jpg" alt="perl" title="perl" width="150" height="150" class="alignleft size-thumbnail wp-image-89" />a word about the power of unix shell commands: perl one liners, i.e. one line of <a href="http://www.perl.org/">perl</a> that can save you hours of error-prone hand changes. when i want to do some massive changes in my code this is what i use.</p>
<p>say you need replcae a record name from abc_record to xyz_record, first if you use perl recursivlly it will touch all files and revision control will makr them for commit. you don&#8217;t want that. so use grep to filter the relevant files:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">&quot;#abc_record\b&quot;</span> . <span style="color: #660033;">-rl</span></pre></div></div>

<p>next, do the replace:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">perl</span> <span style="color: #660033;">-i</span> <span style="color: #660033;">-pe</span> s<span style="color: #000000; font-weight: bold;">/</span><span style="color: #ff0000;">&quot;#abc_record\b&quot;</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #ff0000;">&quot;xyz_record&quot;</span><span style="color: #000000; font-weight: bold;">/</span>g <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">&quot;#abc_record\b&quot;</span> src -rl<span style="color: #000000; font-weight: bold;">`</span></pre></div></div>

<p>note the use of \b to mark a word boundary.</p>
<p>a more complex replace is if you need to change the code from using a record to use a wrapper function. so for exmaple we have the record:</p>

<div class="wp_syntax"><div class="code"><pre class="erlang" style="font-family:monospace;"><span style="color: #014ea4;">-</span><span style="color: #5400b3;">record</span><span style="color: #109ab8;">&#40;</span><span style="color: #d400ed;">abc_record</span><span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#123;</span>fld<span style="color: #109ab8;">&#125;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span></pre></div></div>

<p>and we want to start use a wrapper module:</p>

<div class="wp_syntax"><div class="code"><pre class="erlang" style="font-family:monospace;"><span style="color: #014ea4;">-</span><span style="color: #5400b3;">module</span><span style="color: #109ab8;">&#40;</span>rtools<span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
<span style="color: #014ea4;">-</span><span style="color: #5400b3;">export</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#91;</span>fld<span style="color: #014ea4;">/</span><span style="color: #ff9600;">1</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #ff3c00;">fld</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">R</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span> <span style="color: #45b3e6;">R</span><span style="color: #ff9600;">#</span><span style="color: #d400ed;">abc_record</span><span style="color: #6bb810;">.</span>fld<span style="color: #6bb810;">.</span></pre></div></div>

<p>so we can do it instantly like that:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">perl</span> <span style="color: #660033;">-i</span> <span style="color: #660033;">-pe</span> s<span style="color: #000000; font-weight: bold;">/</span><span style="color: #ff0000;">&quot;\b(\w+)#abc_record\.fld&quot;</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #ff0000;">&quot;rtools:fld(\1)&quot;</span><span style="color: #000000; font-weight: bold;">/</span>g <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">&quot;#abc_record\.fld&quot;</span> src -rl<span style="color: #000000; font-weight: bold;">`</span></pre></div></div>

<p>here we have switched from using A#abc_record.fld to record_tools:fld(A)</p>
<p>another trick is to remove complete lines (!), so for exmaple you can get rid of all the -compile(export_all) you have fullishly inserted into your code:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">perl</span> <span style="color: #660033;">-i</span> <span style="color: #660033;">-nle</span> <span style="color: #ff0000;">'print if !/-compile\(export_all\)/'</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">grep</span> export_all src -rl<span style="color: #000000; font-weight: bold;">`</span></pre></div></div>

<p>one safty tip, omit the -i first to try before you actuall change files.</p>
<p>this was just a learn-by-example, more info can be found at:<br />
<a href="http://sial.org/howto/perl/one-liner">http://sial.org/howto/perl/one-liner</a><br />
<a href="http://www.unixguide.net/unix/perl_oneliners.shtml">http://www.unixguide.net/unix/perl_oneliners.shtml</a>	</p>
]]></content:encoded>
			<wfw:commentRss>http://www.virtual-clouds.com/2009/10/81/magic-perl-one-liners/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
