<?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>My stuff &#187; Perl</title>
	<atom:link href="http://michelhollands.net/blog/index.php/category/perl/feed/" rel="self" type="application/rss+xml" />
	<link>http://michelhollands.net/blog</link>
	<description>Tech and travel tidbits</description>
	<lastBuildDate>Tue, 13 Dec 2011 09:59:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>Asking for a value in Perl</title>
		<link>http://michelhollands.net/blog/2007/09/20/asking-for-a-value-in-perl/</link>
		<comments>http://michelhollands.net/blog/2007/09/20/asking-for-a-value-in-perl/#comments</comments>
		<pubDate>Thu, 20 Sep 2007 10:09:28 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
				<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://michelhollands.net/blog/2007/09/20/asking-for-a-value-in-perl/</guid>
		<description><![CDATA[The following Perl snippet is useful if you want to ask for a value from the user. The get_new_value function takes the variable name and a default value as parameter. These are printed on screen. The default value is used if no input is given. use strict; &#160; sub get_new_value &#123; my&#40;$param_name, $param_current_value&#41; = @_; [...]]]></description>
			<content:encoded><![CDATA[<p>The following Perl snippet is useful if you want to ask for a value from the user. The get_new_value function takes the variable name and a default value as parameter. These are printed on screen. The default value is used if no input is given.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">sub</span> get_new_value <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$param_name</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$param_current_value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;$param_name [$param_current_value] :&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">chomp</span><span style="color: #009900;">&#40;</span><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$input</span><span style="color: #339933;">=</span><span style="color: #009999;">&lt;stdin&gt;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$new_value</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$input</span> <span style="color: #b1b100;">eq</span> <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #339933;">?</span> <span style="color: #0000ff;">$param_current_value</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">$input</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">return</span> <span style="color: #0000ff;">$new_value</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$var</span> <span style="color: #339933;">=</span> get_new_value<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'VAR'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$ENV</span><span style="color: #009900;">&#123;</span>VAR<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The output looks like this (this script was saved to set_params.pl) :</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">  $ <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">VAR</span>=<span style="color: #ff0000;">&quot;/usr/bin&quot;</span>
  $ <span style="color: #c20cb9; font-weight: bold;">perl</span> set_params.pl
  VAR <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #7a0874; font-weight: bold;">&#93;</span> :</pre></div></div>

<p>If no value is given and enter is pressed, $var is set to /usr/bin.</p>
]]></content:encoded>
			<wfw:commentRss>http://michelhollands.net/blog/2007/09/20/asking-for-a-value-in-perl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.403 seconds -->

