<?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>Safer Code - Secure Coding In C \ C++ And More.. &#187; Other Languages</title>
	<atom:link href="http://www.safercode.com/blog/category/other-languages/feed" rel="self" type="application/rss+xml" />
	<link>http://www.safercode.com/blog</link>
	<description>Making Your Code Faster, Stronger, Safer…</description>
	<lastBuildDate>Tue, 05 Jan 2010 07:59:56 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Weird Usage Of &#8220;select&#8221; in perl</title>
		<link>http://www.safercode.com/blog/2009/12/29/perl-select-autoflush.html</link>
		<comments>http://www.safercode.com/blog/2009/12/29/perl-select-autoflush.html#comments</comments>
		<pubDate>Tue, 29 Dec 2009 00:58:18 +0000</pubDate>
		<dc:creator>Shantanu Goel</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Other Languages]]></category>
		<category><![CDATA[autoflush. STDOUT]]></category>
		<category><![CDATA[file-handle autoflush in perl]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[select]]></category>

		<guid isPermaLink="false">http://www.safercode.com/blog/2009/12/29/perl-select-autoflush.html</guid>
		<description><![CDATA[<!-- Powered by Shantz WP Prefix Suffix. Tech Blog: http://tech.shantanugoel.com/ Secure Programming Blog: http://www.safercode.com/blog/ Blog: http://blog.shantanugoel.com/ -->Many times while going through some perl code, you must have come across snippets like &#8220;select((select(fh), $&#124;=1)[0])&#8221; and wondered what this means, even though you might know that:

$&#124;=1 is used for setting autoflush (i.e. unbuffered data output) and that
select is used to set the default output to a given file handle instead of STDOUT

Whenever I [...]]]></description>
			<content:encoded><![CDATA[<!-- Powered by Shantz WP Prefix Suffix. Tech Blog: http://tech.shantanugoel.com/ Secure Programming Blog: http://www.safercode.com/blog/ Blog: http://blog.shantanugoel.com/ --><p style='border:thin dotted black; padding:3mm;background-color: rgb(250,150,250);'><strong><a href="http://feedproxy.google.com/SaferCode" rel="alternate" type="application/rss+xml">Subscribe To Our Feed</a> | <a href="http://twitter.com/safercode" rel="nofollow">Follow Us On Twitter</a> | <a href="http://feedburner.google.com/fb/a/mailverify?uri=safercode" rel="nofollow" target="_blank">Get Updates on Email</a></strong></p>
<p>Many times while going through some perl code, you must have come across snippets like &#8220;select((select(fh), $|=1)[0])&#8221; and wondered what this means, even though you might know that:</p>
<ul>
<li>$|=1 is used for setting autoflush (i.e. unbuffered data output) and that</li>
<li>select is used to set the default output to a given file handle instead of STDOUT</li>
</ul>
<p>Whenever I face any issue with a code fragment, I try to break it down into simpler terms to understand it from the beginning (like I did for my random number generation post). So, these are the steps in which I progressed:</p>
<ul>
<li>select(fh) replaces the STDOUT with fh and returns the old filehandle (i.e. STDOUT).</li>
<li>(select(fh),$|=1) does the above and then sets this new output to autoflush</li>
<li>From perl&#8217;s online docs, I found that the output of the above is a list, the first element of which is the old Filehandle</li>
<li>So, (select(fh),$|=1)[0] gives us STDOUT</li>
<li>Then select(select(fh, $|=1)[0]) basically just sets the default output back to STDOUT</li>
</ul>
<p>So, what is the use of all this. Basically, this is nothing but a trick to set autoflush for any filehandle. Now, there is a very simple way to do this. You just need to include the IO::Handle module (by writing &#8220;use IO::Handle;&#8221; in your script) and then call &#8220;fh-&gt;autoflush(1)&#8221; on your file handle (Use 0 as parameter to disable autoflushing). This is much cleaner although it means longer run times as your script now has to include and compile lot of new lines of code because the module you added.
<p style='border:thin dotted black; padding:3mm;background-color: rgb(250,150,250);'>© <a href="http://www.safercode.com/blog/">Safer Code</a> | <a href="http://www.safercode.com/blog/2009/12/29/perl-select-autoflush.html">Weird Usage Of &#8220;select&#8221; in perl</a></p>



Share and Enjoy:


	<a rel="nofollow" target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F12%2F29%2Fperl-select-autoflush.html&amp;title=Weird%20Usage%20Of%20%22select%22%20in%20perl&amp;bodytext=Many%20times%20while%20going%20through%20some%20perl%20code%2C%20you%20must%20have%20come%20across%20snippets%20like%20%22select%28%28select%28fh%29%2C%20%24%7C%3D1%29%5B0%5D%29%22%20and%20wondered%20what%20this%20means%2C%20even%20though%20you%20might%20know%20that%3A%0D%0A%0D%0A%09%24%7C%3D1%20is%20used%20for%20setting%20autoflush%20%28i.e.%20unbuffered%20data%20output%29" title="Digg"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F12%2F29%2Fperl-select-autoflush.html&amp;title=Weird%20Usage%20Of%20%22select%22%20in%20perl&amp;notes=Many%20times%20while%20going%20through%20some%20perl%20code%2C%20you%20must%20have%20come%20across%20snippets%20like%20%22select%28%28select%28fh%29%2C%20%24%7C%3D1%29%5B0%5D%29%22%20and%20wondered%20what%20this%20means%2C%20even%20though%20you%20might%20know%20that%3A%0D%0A%0D%0A%09%24%7C%3D1%20is%20used%20for%20setting%20autoflush%20%28i.e.%20unbuffered%20data%20output%29" title="del.icio.us"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F12%2F29%2Fperl-select-autoflush.html&amp;t=Weird%20Usage%20Of%20%22select%22%20in%20perl" title="Facebook"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F12%2F29%2Fperl-select-autoflush.html&amp;title=Weird%20Usage%20Of%20%22select%22%20in%20perl" title="StumbleUpon"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F12%2F29%2Fperl-select-autoflush.html&amp;title=Weird%20Usage%20Of%20%22select%22%20in%20perl" title="Reddit"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.printfriendly.com/print?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F12%2F29%2Fperl-select-autoflush.html&amp;partner=sociable" title="Print this article!"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/printfriendly.png" title="Print this article!" alt="Print this article!" class="sociable-hovers" /></a>


<br/><br/>
	Tags: <a href="http://www.safercode.com/blog/tag/autoflush-stdout" title="autoflush. STDOUT" rel="tag nofollow">autoflush. STDOUT</a>, <a href="http://www.safercode.com/blog/tag/file-handle-autoflush-in-perl" title="file-handle autoflush in perl" rel="tag nofollow">file-handle autoflush in perl</a>, <a href="http://www.safercode.com/blog/tag/perl" title="perl" rel="tag nofollow">perl</a>, <a href="http://www.safercode.com/blog/tag/select" title="select" rel="tag nofollow">select</a><br />

	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li>No related posts.</li>
	</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.safercode.com/blog/2009/12/29/perl-select-autoflush.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
