<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.2.2" -->
<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/"
	>

<channel>
	<title>www.dantux.com/weblog</title>
	<link>http://dantux.com/weblog</link>
	<description>it's about life and what we enjoy in it...</description>
	<pubDate>Thu, 04 Nov 2010 08:29:45 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.2</generator>
	<language>en</language>
			<item>
		<title>phpMyAdmin doesn&#8217;t work on MAC (solved)</title>
		<link>http://dantux.com/weblog/2010/11/04/phpmyadmin-doesnt-work-on-mac-solved/</link>
		<comments>http://dantux.com/weblog/2010/11/04/phpmyadmin-doesnt-work-on-mac-solved/#comments</comments>
		<pubDate>Thu, 04 Nov 2010 08:29:45 +0000</pubDate>
		<dc:creator>marian</dc:creator>
		
		<category><![CDATA[General Computers]]></category>

		<category><![CDATA[MAC]]></category>

		<guid isPermaLink="false">http://dantux.com/weblog/2010/11/04/phpmyadmin-doesnt-work-on-mac-solved/</guid>
		<description><![CDATA[I recently tried to install phpMyAdmin on my MAC and I had a hard time getting it to allow user login.
Even though MySQL was working fine (was able to log in at the command line as root), phpMyAdmin would just not log me in. It didn&#8217;t give me any error message whatsoever.
I ended up on [...]]]></description>
			<content:encoded><![CDATA[<p>I recently tried to install phpMyAdmin on my MAC and I had a hard time getting it to allow user login.</p>
<p>Even though MySQL was working fine (was able to log in at the command line as root), phpMyAdmin would just not log me in. It didn&#8217;t give me any error message whatsoever.</p>
<p>I ended up on the irc channel #phpMyAdmin on freenode.net and someone there told me that on MAC, the mysql socket is not what phpmyadmin thinks it&#8217;s default. So in the config.inc.php file, you have to specify the location of the socket.<br />
I found that out by issuing the &#8220;status&#8221; command at the mysql prompt in Terminal:</p>
<blockquote><p> mysql&gt; status;<br />
&#8212;&#8212;&#8212;&#8212;&#8211;<br />
mysql  Ver 14.14 Distrib 5.1.48, for apple-darwin10.3.0 (i386) using readline 5.1</p>
<p>Connection id:		8<br />
Current database:<br />
Current user:		root@localhost<br />
SSL:			Not in use<br />
Current pager:		stdout<br />
Using outfile:		&#8221;<br />
Using delimiter:	;<br />
Server version:		5.1.48 MySQL Community Server (GPL)<br />
Protocol version:	10<br />
Connection:		Localhost via UNIX socket<br />
Server characterset:	latin1<br />
Db     characterset:	latin1<br />
Client characterset:	latin1<br />
Conn.  characterset:	latin1<br />
UNIX socket:		/tmp/mysql.sock<br />
Uptime:			14 hours 32 min 35 sec</p>
<p>Threads: 1  Questions: 49  Slow queries: 0  Opens: 15  Flush tables: 1  Open tables: 8  Queries per second avg: 0.0<br />
&#8212;&#8212;&#8212;&#8212;&#8211;</p></blockquote>
<p>So, in my case, was &#8221;	/tmp/mysql.sock&#8221;</p>
<p>Set also the connection type from tcp to socket and voila! worked.</p>
<p>I hope this will help someone else.</p>
]]></content:encoded>
			<wfw:commentRss>http://dantux.com/weblog/2010/11/04/phpmyadmin-doesnt-work-on-mac-solved/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Install / Configure netatalk on Fedora 13</title>
		<link>http://dantux.com/weblog/2010/10/03/install-configure-netatalk-on-fedora-13/</link>
		<comments>http://dantux.com/weblog/2010/10/03/install-configure-netatalk-on-fedora-13/#comments</comments>
		<pubDate>Sun, 03 Oct 2010 19:52:08 +0000</pubDate>
		<dc:creator>marian</dc:creator>
		
		<category><![CDATA[General Computers]]></category>

		<category><![CDATA[MAC]]></category>

		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://dantux.com/weblog/2010/10/03/install-configure-netatalk-on-fedora-13/</guid>
		<description><![CDATA[I spent a good few days trying to get netatalk working properly in Fedora.
Most instructions found on the net were refering to Ubuntu where you have the option to compile netatalk package with SSL enabled.
Apparently Mac OSX 10.5.6 (not sure about earlier versions), cannot authenticate to netatalk on Linux, if the password is not encrypted. [...]]]></description>
			<content:encoded><![CDATA[<p>I spent a good few days trying to get netatalk working properly in Fedora.</p>
<p>Most instructions found on the net were refering to Ubuntu where you have the option to compile netatalk package with SSL enabled.</p>
<p>Apparently Mac OSX 10.5.6 (not sure about earlier versions), cannot authenticate to netatalk on Linux, if the password is not encrypted. (during my searches I found that you can force your mac to accept clear text passwords, by typing this into Terminal:</p>
<pre>defaults write com.apple.AppleShareClient afp_cleartext_allow -bool true</pre>
<p>).</p>
<p>I finally got netatalk to work well, by compiling the package (downloaded from here: <a href="http://sourceforge.net/projects/netatalk/files/" target="_blank">http://sourceforge.net/projects/netatalk/files/</a>) with the following options:</p>
<pre>./configure --with-bdb=/usr/local/BerkeleyDB.5.1 --enable-redhat --sysconfdir=/etc --enable-krbV-uam</pre>
<p>Yes, I did re-install berkeley database to the latest available, but I am not sure if would have worked with the previous version already in the system.</p>
<p>Here is how I compiled BerkleyDB.5.1</p>
<p>I extracted the archive:</p>
<p>db-5.1.19.tar</p>
<p>changed directory to build_unix and ran the configure command like this:</p>
<style>@font-face {   font-family: "?? ??"; }@font-face {   font-family: "?? ??"; }@font-face {   font-family: "Cambria"; }p.MsoNormal, li.MsoNormal, div.MsoNormal { margin: 0cm 0cm 10pt; font-size: 12pt; font-family: Cambria; }.MsoChpDefault { font-family: Cambria; }.MsoPapDefault { margin-bottom: 10pt; }div.WordSection1 { page: WordSection1; }</style>
<p>   <span style="font-size: 12pt; font-family: Cambria">../dist/configure &#8211;with-mutex=&#8221;x86/gcc-assembly&#8221;</span></p>
<p>After that, run make and make install as root.</p>
<p>That was it. Now I could run  the above command to compile netatalk.</p>
<p>Once I configured the package, I ran &#8220;make &amp; make install&#8221;. All I had to do after that, was just to define my shares in /etc/netatalk/AppleVolumes.default</p>
<pre></pre>
<pre></pre>
]]></content:encoded>
			<wfw:commentRss>http://dantux.com/weblog/2010/10/03/install-configure-netatalk-on-fedora-13/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Mesaj de la Karina</title>
		<link>http://dantux.com/weblog/2010/10/01/mesaj-de-la-karina/</link>
		<comments>http://dantux.com/weblog/2010/10/01/mesaj-de-la-karina/#comments</comments>
		<pubDate>Fri, 01 Oct 2010 17:19:10 +0000</pubDate>
		<dc:creator>marian</dc:creator>
		
		<category><![CDATA[Media]]></category>

		<category><![CDATA[Life]]></category>

		<category><![CDATA[Personal]]></category>

		<guid isPermaLink="false">http://dantux.com/weblog/2010/10/01/mesaj-de-la-karina/</guid>
		<description><![CDATA[Unul dintre obisnuitele declaratii ale Karinei, intr-un stil mai&#8230; neobisnuit  




                         
]]></description>
			<content:encoded><![CDATA[<p>Unul dintre obisnuitele declaratii ale Karinei, intr-un stil mai&#8230; neobisnuit <img src='http://dantux.com/weblog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com /pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" id="VideoPlayer" align="middle" height="555" width="650"></object></p>
<param name="allowScriptAccess" value="*"></param>
<param name="allowFullScreen" value="true"></param>
<param value="high"></param>
<param name="bgcolor" value="#ffffff"></param>                         <embed src="http://dantux.com/main_files/VideoPlayer.swf?video=/main_files/talkingTom.flv" quality="high" bgcolor="#ffffff" name="VideoPlayer" allowscriptaccess="*" allowfullscreen="true" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" align="middle" height="360" width="240"></embed></p>
]]></content:encoded>
			<wfw:commentRss>http://dantux.com/weblog/2010/10/01/mesaj-de-la-karina/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How to concatenate MP3 files</title>
		<link>http://dantux.com/weblog/2010/09/05/how-to-concatenate-mp3-files/</link>
		<comments>http://dantux.com/weblog/2010/09/05/how-to-concatenate-mp3-files/#comments</comments>
		<pubDate>Sun, 05 Sep 2010 19:19:37 +0000</pubDate>
		<dc:creator>marian</dc:creator>
		
		<category><![CDATA[Media]]></category>

		<category><![CDATA[General Computers]]></category>

		<category><![CDATA[MAC]]></category>

		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://dantux.com/weblog/2010/09/05/how-to-concatenate-mp3-files/</guid>
		<description><![CDATA[Instructions
Install mp3wrap, ffmpeg and id3lib  if you don’t have them already. On Linux, you’ll likely just use your  distro’s package manager to do this in a few seconds. On my Mac, I just  installed mp3wrap and id3lib from source (easy “./configure &#38;&#38;  make &#38;&#38; make install”) and used the ffmpeg binary [...]]]></description>
			<content:encoded><![CDATA[<h4>Instructions</h4>
<p>Install <a href="http://mp3wrap.sourceforge.net/">mp3wrap</a>, <a href="http://ffmpeg.org/">ffmpeg</a> and <a href="http://id3lib.sourceforge.net/">id3lib</a>  if you don’t have them already. On Linux, you’ll likely just use your  distro’s package manager to do this in a few seconds. On my Mac, I just  installed mp3wrap and id3lib from source (easy “./configure &amp;&amp;  make &amp;&amp; make install”) and used the <a href="http://www.macosxhints.com/article.php?story=20061220082125312">ffmpeg binary from ffmpegX</a> (since Fink mangled dependencies).</p>
<p>Do this (combines 1.mp3, 2.mp3 and 3.mp3 into all.mp3, and copies ID3 tags from 1.mp3):</p>
<pre><code class="prettyprint lang-sh"><span class="pln">mp3wrap tmp</span><span class="pun">.</span><span class="pln">mp3 </span><span class="lit">1.mp3</span><span class="pln"> </span><span class="lit">2.mp3</span><span class="pln"> </span><span class="lit">3.mp3</span><span class="pln">
ffmpeg </span><span class="pun">-</span><span class="pln">i tmp_MP3WRAP</span><span class="pun">.</span><span class="pln">mp3 </span><span class="pun">-</span><span class="pln">acodec copy all</span><span class="pun">.</span><span class="pln">mp3 </span><span class="pun">&amp;&amp;</span><span class="pln"> rm tmp_MP3WRAP</span><span class="pun">.</span><span class="pln">mp3
id3cp </span><span class="lit">1.mp3</span><span class="pln"> all</span><span class="pun">.</span><span class="pln">mp3</span></code></pre>
<p>Here’s what’s happening:</p>
<ol>
<li>mp3wrap merges the files. Unfortunately, it also leaves wrong file duration headers and clobbers any original ID3 tags.</li>
<li>ffmpeg fixes the file duration. You could also use something else, like mplayer/mencoder (<code>-oac copy</code> option) here.</li>
<li>id3cp copies the ID3 tags from 1.mp3 to all.mp3</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://dantux.com/weblog/2010/09/05/how-to-concatenate-mp3-files/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Learn Perl in 10 easy lessons - Lesson 5</title>
		<link>http://dantux.com/weblog/2010/08/21/learn-perl-in-10-easy-lessons-lesson-5/</link>
		<comments>http://dantux.com/weblog/2010/08/21/learn-perl-in-10-easy-lessons-lesson-5/#comments</comments>
		<pubDate>Sat, 21 Aug 2010 21:17:20 +0000</pubDate>
		<dc:creator>marian</dc:creator>
		
		<category><![CDATA[General Computers]]></category>

		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://dantux.com/weblog/2010/08/21/learn-perl-in-10-easy-lessons-lesson-5/</guid>
		<description><![CDATA[In  the previous lesson we learnt about string comparisons and file parsing.  In this lesson we&#8217;ll see how Perl can interact with the filesystem and  execute commands in the Linux operating system. We&#8217;ll then use what we&#8217;ve  learnt to write a little script.
Executing system commands
Perl  provides a function called &#8220;system&#8221; [...]]]></description>
			<content:encoded><![CDATA[<p>In  the previous lesson we learnt about string comparisons and file parsing.  In this lesson we&#8217;ll see how Perl can interact with the filesystem and  execute commands in the Linux operating system. We&#8217;ll then use what we&#8217;ve  learnt to write a little script.</p>
<h2>Executing system commands</h2>
<p>Perl  provides a function called &#8220;system&#8221; which can execute a command or a set  of commands directly on the operating system. In fact, Perl passes the  command to the operating system, which executes it, and then returns the  result back to Perl.</p>
<p>So, for instance, the following Perl script prints the content of the current directory:</p>
<p style="background-color: #dddddd"> <font face="monospace"><br />
<font color="#a020f0">#!/usr/bin/perl</font><br />
<font color="#804040"><strong>system</strong></font>(<font color="#ff00ff">&#8220;</font><font color="#ff00ff">ls</font><font color="#ff00ff">&#8220;</font>);<br />
</font></p>
<p>What actually happens is that the Unix process which runs the  Perl interpreter forks and the newly created child Unix process executes  the&#8221;ls&#8221; command. When the execution finishes, it returns the exit code of  the command back to the Perl interpreter.</p>
<p>If you are familiar  with Unix commands exit codes you can test the success of the execution  of your command by assigning the return value of &#8220;system&#8221; to a variable,  and then evaluate this variable. For instance:</p>
<p style="background-color: #dddddd"> <font face="monospace"><font color="#008080">$lsExecutedSuccessfully</font> = <font color="#804040"><strong>system</strong></font>(<font color="#ff00ff">&#8220;</font><font color="#ff00ff">ls</font><font color="#ff00ff">&#8220;</font>);</font></p>
<p> Here, if <font face="monospace"><font color="#ff00ff">&#8220;</font><font color="#ff00ff">ls</font><font color="#ff00ff">&#8220;</font></font> executes successfully, the  variable <font face="monospace"><font color="#008080">$lsExecutedSuccessfully</font></font> receives the value 0. This value  corresponds tot he successful exit code of the command <font face="monospace"><font color="#ff00ff">&#8220;</font><font color="#ff00ff">ls</font><font color="#ff00ff">&#8220;</font></font>.</p>
<h3>Executing system commands and capturing the output</h3>
<p>Sometimes,  when you run a Linux command from your Perl script you&#8217;re more  interested in what it writes on the screen than in its exit code. For  instance, when you execute <font face="monospace"><font color="#ff00ff">&#8220;</font><font color="#ff00ff">ls</font><font color="#ff00ff">&#8220;</font></font> you&#8217;re more likely to be interested in  the list of files being printed on the screen than in the value 0  returned by &#8220;system&#8221;.<br />
To achieve this, you can use evaluated  quotes &#8220;`&#8221; instead of &#8220;system&#8221;. Not only does it executes the command, but  it also returns what the commands writes in its standard output:</p>
<p style="background-color: #dddddd"> <font face="monospace"><br />
<font color="#008080">@files</font> = <font color="#804040"><strong>`</strong></font><font color="#ff00ff">ls</font><font color="#804040"><strong>`</strong></font>;</font></p>
<p>In this example, the listing of the files returned by &#8220;ls&#8221; does not  appear on the screen. Instead, it gets stored in the <font face="monospace"><font color="#008080">@files</font></font> array.</p>
<h3>Changing the working directory</h3>
<p>In  the shell you would type &#8220;cd /home&#8221; to change the working directory to  /home. You could write the following in your Perl script:</p>
<p style="background-color: #dddddd"> <font face="monospace"><br />
<font color="#804040"><strong>system</strong></font>(<font color="#ff00ff">&#8220;</font><font color="#ff00ff">cd /home</font><font color="#ff00ff">&#8220;</font>);</font></p>
<p>But it would have no effect. In fact, since the system call forks from  the process used by the interpreter, it doesn&#8217;t change the  working directory of your script. Instead, you can use a Perl function  called &#8220;chdir&#8221;:</p>
<p style="background-color: #dddddd"> <font face="monospace"><br />
<font color="#804040"><strong>chdir</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">/home</font><font color="#ff00ff">&#8220;</font>;</font></p>
<h3>Interacting with the filesystem</h3>
<p>Perl provides a lot of  functions to interact with the files and directories of your filesystem.  Here are some of these handy functions:</p>
<h4>chmod</h4>
<p>&#8220;chmod&#8221;  changes the permissions of a file or a list of files and returns the  number of files that were changed. The first argument must be the  numerical mode.<br />
Examples:</p>
<p style="background-color: #dddddd"> <font face="monospace"><br />
<font color="#804040"><strong>chmod</strong></font> <font color="#ff00ff">0777</font>, <font color="#ff00ff">&#8220;</font><font color="#ff00ff">/home/clem/program.pl</font><font color="#ff00ff">&#8220;</font>;<br />
<font color="#804040"><strong>chmod</strong></font> <font color="#ff00ff">0777</font>, <font color="#008080">@myFiles</font>;</font></p>
<h4>symlink</h4>
<p>&#8220;symlink&#8221; creates a symbolic link. It is the  equivalent of &#8220;ln -s&#8221;.The first argument is the file name, the second  argument is the link name.<br />
Example:</p>
<p style="background-color: #dddddd"> <font face="monospace"><br />
<font color="#804040"><strong>symlink</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">/home/clem/program.pl</font><font color="#ff00ff">&#8220;</font>, <font color="#ff00ff">&#8220;</font><font color="#ff00ff">/usr/bin/program</font><font color="#ff00ff">&#8220;</font>;<br />
</font></p>
<h4>mkdir</h4>
<p>&#8220;mkdir&#8221; creates a directory. The first argument is the  name of the directory and the second argument is the octal mode which  defines the permissions for that directory.<br />
For example:</p>
<p style="background-color: #dddddd"> <font face="monospace"><br />
<font color="#804040"><strong>mkdir</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">/home/clem/perl_programs</font><font color="#ff00ff">&#8220;</font>, <font color="#ff00ff">0664</font>;</font></p>
<h4>rename</h4>
<p>&#8220;rename&#8221; is the equivalent of &#8220;mv&#8221; in Unix. It renames or moves a file.<br />
Example:</p>
<p style="background-color: #dddddd"> <font face="monospace"><br />
<font color="#804040"><strong>rename</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">/home/clem/program.pl</font><font color="#ff00ff">&#8220;</font>, <font color="#ff00ff">&#8220;</font><font color="#ff00ff">/home/clem/program</font><font color="#ff00ff">&#8220;</font>;</font></p>
<h4>rmdir</h4>
<p>&#8220;rmdir&#8221; deletes a directory, but only if it is empty.</p>
<p>Example:</p>
<p style="background-color: #dddddd"> <font face="monospace"><br />
<font color="#804040"><strong>rmdir</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">/home/clem/perl_programs</font><font color="#ff00ff">&#8220;</font>;</font></p>
<h4>stat</h4>
<p>&#8220;stat&#8221; returns a 13-element array which represent the properties of a file. The elements of the array are :</p>
<p>0:  $dev, 1: $ino, 2: $mode, 3: $nlink, 4: $uid, 5: $gid, 6: $rdev,7:  $size, 8: $atime, 9: $mtime, 10: $ctime, 11: $blksize, 12: $blocks</p>
<p>Example:</p>
<p style="background-color: #dddddd"> <font face="monospace"><br />
<font color="#804040"><strong>stat</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">/home/clem/program.pl</font><font color="#ff00ff">&#8220;</font>;</font></p>
<h4>unlink</h4>
<p>&#8220;unlink&#8221; deletes a file or a list of files.<br />
Example:</p>
<p style="background-color: #dddddd"> <font face="monospace"><br />
<font color="#804040"><strong>unlink</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">/home/clem/program.pl</font><font color="#ff00ff">&#8220;</font>;</font></p>
<h3>Perl Script Exercise: Netswitch</h3>
<p>In this exercise, we want to  be able to switch between networks. Wedefined network configuration  files in a directory called &#8220;networks&#8221;.</p>
<p>For instance, here is the content of ./networks/home:<tt> </tt><br />
<tt>interface=eth2</tt><br />
<tt>type=dhcp</tt><br />
<tt>proxy=none</tt><br />
And here is the content of ./networks/work:<tt> </tt><br />
<tt>interface=eth1</tt><br />
<tt>type=dhcp</tt><br />
<tt>proxy=www-proxy.work.com</tt><br />
<tt>proxy_port=8080</tt><br />
The  following Perl script takes a network name as its command lineargument,  opens the file with the same name from ./networks and setsthe network  interface with the data taken from the content of that file:</p>
<p style="background-color: #dddddd"> <font face="monospace"><br />
<font color="#a020f0">#!/usr/bin/perl</font><br />
<font color="#0000ff">#default values</font><br />
<font color="#008080">$interface</font>=<font color="#ff00ff">&#8220;</font><font color="#ff00ff">none</font><font color="#ff00ff">&#8220;</font>;<br />
<font color="#008080">$type</font>=<font color="#ff00ff">&#8220;</font><font color="#ff00ff">none</font><font color="#ff00ff">&#8220;</font>;<br />
<font color="#008080">$address</font>=<font color="#ff00ff">&#8220;</font><font color="#ff00ff">none</font><font color="#ff00ff">&#8220;</font>;<br />
<font color="#008080">$gateway</font>=<font color="#ff00ff">&#8220;</font><font color="#ff00ff">none</font><font color="#ff00ff">&#8220;</font>;<br />
<font color="#008080">$dns</font>=<font color="#ff00ff">&#8220;</font><font color="#ff00ff">none</font><font color="#ff00ff">&#8220;</font>;<br />
<font color="#008080">$proxy</font>=<font color="#ff00ff">&#8220;</font><font color="#ff00ff">none</font><font color="#ff00ff">&#8220;</font>;<br />
<font color="#008080">$proxy_port</font> = <font color="#ff00ff">&#8220;</font><font color="#ff00ff">none</font><font color="#ff00ff">&#8220;</font>;<br />
<font color="#0000ff">#gather information from the network file</font><br />
<font color="#008080">$network</font> = <font color="#008080">$1</font>;<br />
<font color="#008080">$networkFile</font> = <font color="#ff00ff">&#8220;</font><font color="#ff00ff">./networks/</font><font color="#008080">$network</font><font color="#ff00ff">&#8220;</font>;</font><br />
<font face="monospace"><font color="#804040"><strong>open</strong></font> (<font color="#008080">NETWORK</font>, <font color="#ff00ff">&#8220;</font><font color="#008080">$networkFile</font><font color="#ff00ff">&#8220;</font>) <font color="#804040"><strong>or</strong></font> <font color="#804040"><strong>die</strong></font> <font color="#ff00ff">&#8220;</font><font color="#008080">$networkFile</font><font color="#ff00ff"> not found or not readable</font><font color="#6a5acd">\n</font><font color="#ff00ff">&#8220;</font>;<br />
<font color="#804040"><strong>while</strong></font> (<font color="#008080">$line</font> = <font color="#008080">&lt;NETWORK&gt;</font>) {<br />
<font color="#804040"><strong>chomp</strong></font> <font color="#008080">$line</font>;<br />
(<font color="#008080">$variable</font>, <font color="#008080">$value</font>) = <font color="#804040"><strong>split</strong></font><font color="#804040"><strong> /</strong></font><font color="#ff00ff">=</font><font color="#804040"><strong>/</strong></font>, <font color="#008080">$line</font>;<br />
<font color="#804040"><strong>if</strong></font> (<font color="#008080">$variable</font> <font color="#804040"><strong>eq</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">interface</font><font color="#ff00ff">&#8220;</font>) <font color="#008080">$interface</font> = <font color="#008080">$value</font>;<br />
<font color="#804040"><strong>elsif</strong></font> (<font color="#008080">$variable</font> <font color="#804040"><strong>eq</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">type</font><font color="#ff00ff">&#8220;</font>) <font color="#008080">$type</font> = <font color="#008080">$value</font>;<br />
<font color="#804040"><strong>elsif</strong></font> (<font color="#008080">$variable</font> <font color="#804040"><strong>eq</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">address</font><font color="#ff00ff">&#8220;</font>) <font color="#008080">$address</font> = <font color="#008080">$value</font>;<br />
<font color="#804040"><strong>elsif</strong></font> (<font color="#008080">$variable</font> <font color="#804040"><strong>eq</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">gateway</font><font color="#ff00ff">&#8220;</font>) <font color="#008080">$gateway</font> = <font color="#008080">$value</font>;<br />
<font color="#804040"><strong>elsif</strong></font> (<font color="#008080">$variable</font> <font color="#804040"><strong>eq</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">dns</font><font color="#ff00ff">&#8220;</font>) <font color="#008080">$dns</font> = <font color="#008080">$value</font>;<br />
<font color="#804040"><strong>elsif</strong></font> (<font color="#008080">$variable</font> <font color="#804040"><strong>eq</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">proxy</font><font color="#ff00ff">&#8220;</font>) <font color="#008080">$proxy</font> = <font color="#008080">$value</font>;<br />
<font color="#804040"><strong>elsif</strong></font> (<font color="#008080">$variable</font> <font color="#804040"><strong>eq</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">proxy_port</font><font color="#ff00ff">&#8220;</font>) <font color="#008080">$proxy_port</font> = <font color="#008080">$value</font>;<br />
}<br />
<font color="#0000ff">#make sure the type and interface are defined</font><br />
<font color="#804040"><strong>if</strong></font> (<font color="#008080">$interface</font> <font color="#804040"><strong>eq</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">none</font><font color="#ff00ff">&#8220;</font>) <font color="#804040"><strong>die</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">Interface name not defined</font><font color="#6a5acd">\n</font><font color="#ff00ff">&#8220;</font>;<br />
<font color="#804040"><strong>if</strong></font> (<font color="#008080">$type</font> <font color="#804040"><strong>eq</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">none</font><font color="#ff00ff">&#8220;</font>) <font color="#804040"><strong>die</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">Network type (dhcp, static) not defined</font><font color="#6a5acd">\n</font><font color="#ff00ff">&#8220;</font>;<br />
<font color="#804040"><strong>if</strong></font> (<font color="#008080">$type</font> <font color="#804040"><strong>eq</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">dhcp</font><font color="#ff00ff">&#8220;</font>) {<br />
<font color="#804040"><strong>print</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">Network type: dhcp</font><font color="#6a5acd">\n</font><font color="#ff00ff">&#8220;</font>;<br />
<font color="#0000ff">#just get an IP address and settings from the DHCP Server</font><br />
<font color="#804040"><strong>system</strong></font>(<font color="#ff00ff">&#8220;</font><font color="#ff00ff">dhclient</font><font color="#ff00ff">&#8220;</font>);<br />
}<br />
<font color="#804040"><strong>elsif</strong></font> (<font color="#008080">$type</font> <font color="#804040"><strong>eq</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">static</font><font color="#ff00ff">&#8220;</font>) {<br />
<font color="#804040"><strong>print</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">Network type: static</font><font color="#6a5acd">\n</font><font color="#ff00ff">&#8220;</font>;<br />
<font color="#0000ff">#bring the interface up</font><br />
<font color="#804040"><strong>if</strong></font> (<font color="#008080">$address</font> <font color="#804040"><strong>eq</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">none</font><font color="#ff00ff">&#8220;</font>) <font color="#804040"><strong>die</strong></font> (<font color="#ff00ff">&#8220;</font><font color="#ff00ff">IP address not defined</font><font color="#6a5acd">\n</font><font color="#ff00ff">&#8220;</font>);<br />
<font color="#804040"><strong>system</strong></font>(<font color="#ff00ff">&#8220;</font><font color="#ff00ff">ifconfig </font><font color="#008080">$interface</font><font color="#ff00ff"> </font><font color="#008080">$address</font><font color="#ff00ff"> up</font><font color="#ff00ff">&#8220;</font>);<br />
<font color="#804040"><strong>if</strong></font> (<font color="#008080">$gateway</font> <font color="#804040"><strong>ne</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">none</font><font color="#ff00ff">&#8220;</font>) {<br />
<font color="#804040"><strong>print</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">Gateway: </font><font color="#008080">$gateway</font><font color="#6a5acd">\n</font><font color="#ff00ff">&#8220;</font>;<br />
<font color="#804040"><strong>system</strong></font>(<font color="#ff00ff">&#8220;</font><font color="#ff00ff">route add default gw </font><font color="#008080">$gateway</font><font color="#ff00ff">&#8220;</font>);<br />
}<br />
<font color="#804040"><strong>if</strong></font> (<font color="#008080">$dns</font> <font color="#804040"><strong>ne</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">none</font><font color="#ff00ff">&#8220;</font>) {<br />
<font color="#804040"><strong>print</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">DNS Server: </font><font color="#008080">$dns</font><font color="#6a5acd">\n</font><font color="#ff00ff">&#8220;</font>;<br />
<font color="#008080">$strNameServer</font> = <font color="#ff00ff">&#8220;</font><font color="#ff00ff">cat </font><font color="#ff00ff">&#8220;</font>.<font color="#ff00ff">&#8220;</font><font color="#ff00ff">&#8216;</font><font color="#ff00ff">&#8220;</font>.<font color="#ff00ff">&#8220;</font><font color="#ff00ff">nameserver </font><font color="#008080">$dns</font><font color="#ff00ff">&#8220;</font>.<font color="#ff00ff">&#8220;</font><font color="#ff00ff">&#8216; &gt; /etc/resolv.conf</font><font color="#ff00ff">&#8220;</font>;<br />
<font color="#804040"><strong>system</strong></font>(<font color="#008080">$strNameServer</font>);<br />
}<br />
}<br />
<font color="#804040"><strong>else</strong></font> <font color="#804040"><strong>die</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">Bad network type : </font><font color="#008080">$type</font><font color="#ff00ff">. Use dhcp or static.</font><font color="#6a5acd">\n</font><font color="#ff00ff">&#8220;</font>;</font></p>
<p>Try to understand each line of that script. The script doesn&#8217;t setthe  proxy in APT, Firefox&#8230;etc. See if you can update the script toadd such  functionality. Also, it would be great if the script couldlist the  possible networks available when the user types &#8220;netswitch-l&#8221;. As there  are many ways to solve a problem, especially in Perl,please post your  solutions and ideas. Together you should be able towrite a great network  switcher.</p>
]]></content:encoded>
			<wfw:commentRss>http://dantux.com/weblog/2010/08/21/learn-perl-in-10-easy-lessons-lesson-5/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Learn Perl in 10 easy lessons - Lesson 4</title>
		<link>http://dantux.com/weblog/2010/08/21/learn-perl-in-10-easy-lessons-lesson-4/</link>
		<comments>http://dantux.com/weblog/2010/08/21/learn-perl-in-10-easy-lessons-lesson-4/#comments</comments>
		<pubDate>Sat, 21 Aug 2010 20:10:32 +0000</pubDate>
		<dc:creator>marian</dc:creator>
		
		<category><![CDATA[MAC]]></category>

		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://dantux.com/weblog/2010/08/21/learn-perl-in-10-easy-lessons-lesson-4/</guid>
		<description><![CDATA[In  the previous lesson we learnt about If statements and loops. We also  started to study how to open files and how to write and read into them.  In this lesson we will learn how to compare strings and  we will study  some advanced techniques for parsing files.
String comparisons
The =~ [...]]]></description>
			<content:encoded><![CDATA[<p>In  the previous lesson we learnt about If statements and loops. We also  started to study how to open files and how to write and read into them.  In this lesson we will learn how to compare strings and  we will study  some advanced techniques for parsing files.</p>
<h2>String comparisons</h2>
<h3>The =~ operator</h3>
<p>Perl  provides an operator which you&#8217;ll find very useful to parse and search  files: &#8220;=~&#8221;. If you are not familiar with this operator, think of it as a  &#8220;contains&#8221; operator. For instance:<br />
<font size="2"><br />
</font><font style="font-family: monospace">&#8220;hello world&#8221; =~ &#8220;world&#8221;</font> returns true, as &#8220;hello world&#8221; contains &#8220;world&#8221;. <font size="2"><br />
</font><font style="font-family: monospace">&#8220;hello world&#8221; =~ &#8220;o worl&#8221;</font> also returns true since &#8220;o worl&#8221; is included in the string &#8220;hello world&#8221;.<font size="2"><br />
</font><font style="font-family: monospace">&#8220;hello world&#8221; =~ &#8220;wrld&#8221;</font> returns false because the string &#8220;hello world&#8221; does not contain &#8220;wrld&#8221;.</p>
<p>Using  the =~ operator you can easily test if a variable contains a particular  string, and this will help you a lot while parsing text files. You can  also use regular expressions in conjunction with the =~ operator.  Although it is too early at this stage to study regular expressions in  details, here are some techniques that you can use with =~.</p>
<p>We  replace the double quotes by forward slashes in order to tell our =~  operator that we&#8217;re not simply looking for a string anymore but for a  matching pattern(with a bit of logic inside it):<br />
<font size="2"><br />
</font><span style="font-family: monospace">&#8220;hello world&#8221; =~ &#8220;world&#8221;</span><font face="Courier 10 Pitch"> </font>is the same as <font style="font-family: monospace">&#8220;hello world&#8221; =~ /world/</font></p>
<p>Although&#8221;world&#8221;  represents a string and /world/ represents an expression, these two  instructions return true. By adding logic to the expression, we can  refine the meaning of our =~ operator.</p>
<h3><font face="Helvetica, Arial, sans-serif"><font size="3">=~/^Starts with/</font></font></h3>
<p>A leading ^ sign changes the meaning of the operator from&#8221;contains&#8221; to &#8220;starts with&#8221;:<font size="2"><br />
</font><br />
<font style="font-family: monospace">&#8220;hello world&#8221; =~ /world/</font> returns true because &#8220;hello world&#8221;contains &#8220;world&#8221;.<br />
<font style="font-family: monospace">&#8220;hello world&#8221; =~ /^world/</font> returns false since &#8220;hello world&#8221;doesn&#8217;t start with &#8220;world&#8221;.<br />
<font style="font-family: monospace">&#8220;hello world&#8221; =~ /^hell/</font>returns true because &#8220;hello world&#8221; starts with&#8221;hell&#8221;.</p>
<h3>=~ /Ends with$/</h3>
<p>By adding a $ sign in the end of the expression you can change the meaning of the operator from &#8220;contains&#8221; to &#8220;ends with&#8221;:</p>
<p><font style="font-family: monospace">&#8220;hello world&#8221;=~ /world/</font> returns true because &#8220;hello world&#8221;contains &#8220;world&#8221;.<br />
<font style="font-family: monospace">&#8220;hello world&#8221; =~ /world$/</font> also returns true, but this time it&#8217;s because &#8220;hello world&#8221; ends with &#8220;world&#8221;.<br />
<font style="font-family: monospace">&#8220;hello world&#8221; =~ /hello$/</font> returns false, because &#8220;hello world&#8221; doesn&#8217;t end with &#8220;hello&#8221;.</p>
<h3>The &#8220;eq&#8221; and &#8220;ne&#8221; operators</h3>
<p style="margin-bottom: 0cm">You  can use both the ^ and $ signs in the same expression, and it would mean  that you&#8217;re looking for a string with which your variable would both  starts and end. For instance:</p>
<p><font style="font-family: monospace">&#8220;hello world&#8221;=~ /^hello world$/</font> returns true because &#8220;hello world&#8221;starts and ends with &#8220;hello world&#8221;.<br />
<font style="font-family: monospace">&#8220;hello world&#8221; =~ /^hello$/</font> returns false, because although&#8221;hello world&#8221; starts with &#8220;hello&#8221; it doesn&#8217;t end with it..</p>
<p>Note  that there is no much point using both ^ and $in the same expression.  If you&#8217;re string starts and ends with something it is likely to be equal  to that something&#8230; if you wantto test the equality of two strings, you  can simply use the eq operator:</p>
<p><font style="font-family: monospace">&#8220;hello world&#8221; eq &#8220;hello world&#8221;</font> returns true because the two strings are identical.</p>
<p style="margin-bottom: 0cm">The ne operator tests the non-equality of two strings. It returns true if the strings are different and false otherwise:</p>
<p style="margin-bottom: 0cm"><font style="font-family: monospace">&#8220;</font><font style="font-family: monospace">hello world</font><font style="font-family: monospace">&#8220;</font><font style="font-family: monospace"> ne </font><font style="font-family: monospace">&#8220;</font><font style="font-family: monospace">good night</font><font style="font-family: monospace">&#8220;</font> returns true.</p>
<p style="margin-bottom: 0cm"><font style="font-family: monospace">&#8220;</font><span style="font-family: monospace">hello world</span><font style="font-family: monospace">&#8220;</font><span style="font-family: monospace"> ne </span><font style="font-family: monospace">&#8220;</font><span style="font-family: monospace">Hello worlD</span><font style="font-family: monospace">&#8220;</font><font face="Courier 10 Pitch"> </font>returns true (remember that Perl is case-sensitive)</p>
<p style="margin-bottom: 0cm"><font style="font-family: monospace">&#8220;</font><font style="font-family: monospace">hello world</font><font style="font-family: monospace">&#8220;</font><font style="font-family: monospace"> ne </font><font style="font-family: monospace">&#8220;</font><font style="font-family: monospace">hello world</font><font style="font-family: monospace">&#8220;</font> returns false because both strings are the same.</p>
<p style="margin-bottom: 0cm">Remember  to use the eq and ne operators to test the equality of strings in Perl,  and their equivalence == and!= to test numerical values.</p>
<h3>The !~ operator</h3>
<p>The !~ operator is used as a &#8220;does not contain&#8221; operator. What!= is to ==, ne is to eq and !~ is to =~. For instance:</p>
<p><font style="font-family: monospace">&#8220;</font><font style="font-family: monospace">hello world</font><font style="font-family: monospace">&#8220;</font><font style="font-family: monospace"> !~ </font><font style="font-family: monospace">&#8220;</font><font style="font-family: monospace">world</font><font style="font-family: monospace">&#8220;</font> returns false because &#8220;hello  world&#8221; does contain &#8220;world&#8221;.</p>
<p><font style="font-family: monospace">&#8220;</font><font style="font-family: monospace">hello world</font><font style="font-family: monospace">&#8220;</font><font style="font-family: monospace"> !~ </font><font style="font-family: monospace">&#8220;</font><font style="font-family: monospace">wwt</font><font style="font-family: monospace">&#8220;</font> returns true because &#8220;hello world&#8221; does not contain &#8220;wwt&#8221;.</p>
<h3>Case insensitive search</h3>
<p>When you use the =~ operator you test the matching of a string within another, this is always case sensitive. For instance:</p>
<p><font style="font-family: monospace">&#8220;</font><font style="font-family: monospace">hello world</font><font style="font-family: monospace">&#8220;</font><font style="font-family: monospace"> =~ </font><font style="font-family: monospace">&#8220;</font><font style="font-family: monospace">world</font><font style="font-family: monospace">&#8221; </font>returns true.</p>
<p><font style="font-family: monospace">&#8220;</font><font style="font-family: monospace">hello world</font><font style="font-family: monospace">&#8220;</font><font style="font-family: monospace"> =~ </font><font style="font-family: monospace">&#8220;</font><font style="font-family: monospace">woRld</font><font style="font-family: monospace">&#8221; </font>returns false.</p>
<p>If you want to make the =~ operator insensitive, add an &#8220;i&#8221; after the expression:</p>
<p><font style="font-family: monospace">&#8220;</font><font style="font-family: monospace">hello world</font><font style="font-family: monospace">&#8220;</font><font style="font-family: monospace"> =~ /world/i </font>returns true.</p>
<p><font style="font-family: monospace">&#8220;</font><font style="font-family: monospace">hello world</font><font style="font-family: monospace">&#8220;</font><font style="font-family: monospace"> =~ /woRld/i </font>also returns true.</p>
<h2>Substitutions</h2>
<p>The  =~ operator can also be used to find occurrences of a string within a  variable and substitute them with another string. For instance, if you  have a variable which contains text, and you want to change all  occurrences of &#8220;aaa&#8221; with &#8220;aab&#8221; within that text, you can simply use the  following substitution:</p>
<p style="font-family: monospace">$variable =~ s/aaa/aab/;</p>
<p>All occurrences of &#8220;aaa&#8221; within $variable will then be changed to &#8220;aab&#8221;. Note that we prefixed our expression with an &#8220;s&#8221; to change the meaning of the operator from &#8220;contains&#8221; to &#8220;substitute&#8221;.</p>
<h2>Parsing files</h2>
<p>There  are many ways to parse a text file. In Perl, if the file has its data  organized line by line with delimiters, it is very easy to parse it.</p>
<p>Let&#8217;s  study a simple example. We have a set of employees in a file called  employees.txt. In this file, each line represents an employee.The  information relative to each employee is delimited with tabs, the first  column is the name of the employee, the second column indicates his  department and the third one his salary. Here is an overview of the file:</p>
<p style="font-family: monospace"><font size="3">Mr John Doe     R&amp;D    21000<br />
Miss Gloria Dunne      HR      23000<br />
Mr Jack Stevens HR     45000<br />
Mrs Julie Fay   R&amp;D    30000<br />
Mr Patrick Reed R&amp;D    33000</font></p>
<p>In  order to obtain some statistics, the HR department wants to establish a  list of all male employees who work in the R&amp;D department and which  salary is more than 25000.</p>
<p>To obtain this list, we design a simple Perl script, which:</p>
<ol>
<li>opens the employees.txt file</li>
<li>loops through each line</li>
<li>identifies the name, department and salary of the employee</li>
<li>ignores and goes to the next line if the employee is female	(the name does not start with Mr)</li>
<li>ignores and goes to the next line if the salary is less or	equal to 25000.</li>
<li>ignores and goes to the next line if the department is not &#8220;R&amp;D&#8221;.</li>
<li>prints the name and the salary of the employee on the screen.</li>
</ol>
<p>To do this, we&#8217;ll introduce two Perl functions:</p>
<ul>
<li>&#8220;chomp&#8221;  is used to remove the carriage return found in	the end of the line. For  instance chomp $variable removes all	carriage returns in the variable.</li>
<li> &#8220;split&#8221; is used to cut the line in different  parts where	it finds a delimiter. For instance split /o/, &#8220;hello world&#8221;	 returns an array containing &#8220;hell&#8221;, &#8220;w&#8221; and &#8220;rld&#8221;. In	our example we&#8217;ll  split the lines with the tab delimiter, which in	Perl is written &#8220;t&#8221;.</li>
</ul>
<p>Here is the script which establishes the list of  male employees from the R&amp;D department with a salary greater than  25000. To make things a bit clearer, comments were introduced within the  scripts(comments in Perl start with a # sign):</p>
<p style="background-color: #dddddd"> <font color="#0000ff">#open the employees file</font><br />
<font color="#804040"><strong>open</strong></font> (<font color="#008080">EMPLOYEES</font>,<font color="#ff00ff">&#8220;</font><font color="#ff00ff">employees.txt</font><font color="#ff00ff">&#8220;</font>);<br />
<font color="#0000ff">#for each line</font><br />
<font color="#804040"><strong>while</strong></font> (<font color="#008080">$line</font> =<font color="#008080">&lt;EMPLOYEES&gt;</font>) {<br />
<font color="#0000ff">#remove the carriage return</font><br />
<font color="#804040"><strong>chomp</strong></font> <font color="#008080">$line</font>;<br />
<font color="#0000ff">#split the line between tabs</font><br />
<font color="#0000ff">#and get the different elements</font><br />
(<font color="#008080">$name</font>, <font color="#008080">$department</font>, <font color="#008080">$salary</font>) = <font color="#804040"><strong>split</strong></font><font color="#804040"><strong> /</strong></font><font color="#ff00ff">t</font><font color="#804040"><strong>/</strong></font>, <font color="#008080">$line</font>;<br />
<font color="#0000ff">#go to the next line unless the name starts with &#8220;Mr &#8220;</font><br />
<font color="#804040"><strong>next</strong></font> <font color="#804040"><strong>unless</strong></font> <font color="#008080">$name</font> =~<font color="#804040"><strong> /</strong></font><font color="#ff00ff">^Mr </font><font color="#804040"><strong>/</strong></font>;<br />
<font color="#0000ff">#go to the next line unless the salary is more than 25000.</font><br />
<font color="#804040"><strong>next</strong></font> <font color="#804040"><strong>unless</strong></font> <font color="#008080">$salary</font> &gt; <font color="#ff00ff">25000</font>;<br />
<font color="#0000ff">#go to the next line unless the department is R&amp;D.</font><br />
<font color="#804040"><strong>next</strong></font> <font color="#804040"><strong>unless</strong></font> <font color="#008080">$department</font> <font color="#804040"><strong>eq</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">R&amp;D</font><font color="#ff00ff">&#8220;</font>;<br />
<font color="#0000ff">#since all employees here are male,</font><br />
<font color="#0000ff">#remove the particle in front of their name</font><br />
<font color="#008080">$name</font> =~ <font color="#804040"><strong>s/</strong></font><font color="#ff00ff">Mr</font><font color="#804040"><strong>//</strong></font>;<br />
<font color="#804040"><strong>print</strong></font><font color="#ff00ff">&#8220;</font><font color="#008080">$name</font><font color="#6a5acd">\n</font><font color="#ff00ff">&#8220;</font>;<br />
}<br />
<font color="#804040"><strong>close</strong></font> (<font color="#008080">EMPLOYEES</font>);</p>
<p style="margin-bottom: 0cm">Study  the script carefully and make sure you understand every part of it. Each  instruction was either explained in this lesson or in one of the  previous ones.</p>
<p style="margin-bottom: 0cm">In the next lesson we&#8217;ll look at how to interact with the filesystem and the Linux operating system from our Perl scripts.</p>
<p style="margin-bottom: 0cm">&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://dantux.com/weblog/2010/08/21/learn-perl-in-10-easy-lessons-lesson-4/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Learn Perl in 10 easy lessons - Lesson 3</title>
		<link>http://dantux.com/weblog/2010/08/21/learn-perl-in-10-easy-lessons-lesson-3/</link>
		<comments>http://dantux.com/weblog/2010/08/21/learn-perl-in-10-easy-lessons-lesson-3/#comments</comments>
		<pubDate>Sat, 21 Aug 2010 19:32:26 +0000</pubDate>
		<dc:creator>marian</dc:creator>
		
		<category><![CDATA[General Computers]]></category>

		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://dantux.com/weblog/2010/08/21/learn-perl-in-10-easy-lessons-lesson-3/</guid>
		<description><![CDATA[In  the previous lesson we learnt what variables were and how to use  command line arguments in Perl. We also wrote our second Perl script:  the Calculator.  In this lesson we will learn how to use If statements  and loops and how to open files to read and write into [...]]]></description>
			<content:encoded><![CDATA[<p>In  the previous lesson we learnt what variables were and how to use  command line arguments in Perl. We also wrote our second Perl script:  the Calculator.  In this lesson we will learn how to use If statements  and loops and how to open files to read and write into them.</p>
<h1>What are If statements and loops?</h1>
<p>Every  Perl script is made of instructions which tell the interpreter what to  do. Depending on the purpose of the script you&#8217;ll write different  instructions. Sometimes, you might want some instructions to be executed  only on certain conditions. To do this,you&#8217;ll use If statements. A  typical If statement is an instruction which defines three elements:</p>
<ul>
<li>A condition</li>
<li>A set of instruction to execute if the condition is met</li>
<li>A set of instruction to execute if the condition is not met</li>
</ul>
<p style="margin-bottom: 0cm">For instance,consider the following code:</p>
<p style="background-color: #dddddd"> <font color="#804040"><strong>if</strong></font> (<font color="#008080">$time</font> &gt; <font color="#ff00ff">12</font>){<br />
<font color="#804040"><strong>print</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">Good Afternoon</font><font color="#ff00ff">&#8220;</font>;<br />
}<font color="#804040"><strong>else</strong></font> {<br />
<font color="#804040"><strong>print</strong></font><font color="#ff00ff">&#8220;</font><font color="#ff00ff">Good Morning</font><font color="#ff00ff">&#8220;</font>;<br />
}</p>
<p>In  this If statement, the condition is (<code>$time &gt; 12</code>). If the condition is  met then the script prints &#8220;<code>Good afternoon</code>&#8220;, otherwise it prints &#8220;<code>Good  morning</code>&#8220;.</p>
<p>Sometimes, you might want some instructions to be  executed more than once. For this you can use loops. A typical loop is  composed of a condition and a set of instructions to execute. There are  different kinds of loops and we will study them in the chapters below.</p>
<h1 class="western">If statements in Perl</h1>
<p>Perl is a very flexible language and this is particularly true when it comes to If statements.</p>
<h2 class="western">Simple &#8220;if&#8221; statements</h2>
<p>The  most common form of If statement in programming is composed of a  condition (between parenthesis) followed by a set of instructions which  are placed between brackets. For instance:</p>
<p style="background-color: #dddddd"> <font color="#804040"><strong>if</strong></font> (<font color="#008080">$time</font> &gt; <font color="#ff00ff">12</font>) {<br />
<font color="#804040"><strong>print</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">time is </font><font color="#008080">$time</font><font color="#6a5acd">\n</font><font color="#ff00ff">&#8220;</font>;<br />
<font color="#804040"><strong>print</strong></font><font color="#ff00ff">&#8220;</font><font color="#ff00ff">Good Afternoon</font><font color="#6a5acd">\n</font><font color="#ff00ff">&#8220;</font>;<br />
}</p>
<p>Although it is considered bad practice, you don&#8217;t have to write the brackets if there is only one instruction in the If statement:</p>
<p style="background-color: #dddddd"> <font color="#804040"><strong>if</strong></font> (<font color="#008080">$time</font> &gt; <font color="#ff00ff">12</font>)<br />
<font color="#804040"><strong>print</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">Good Afternoon</font><font color="#6a5acd">\n</font><font color="#ff00ff">&#8220;</font>;</p>
<p>To  avoid any possible confusion, it is recommended to always write the  brackets, or, if there is only one simple instruction to place the  condition and the instruction on the same line:</p>
<p style="background-color: #dddddd"> <font color="#804040"><strong>if</strong></font>(<font color="#008080">$time</font> &gt; <font color="#ff00ff">12</font>) <font color="#804040"><strong>print</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">Good Afternoon</font><font color="#6a5acd">\n</font><font color="#ff00ff">&#8220;</font>;</p>
<p>In Perl, you can also write the condition after the instruction itself. For instance:</p>
<p style="background-color: #dddddd"> <font color="#804040"><strong>print</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">Good Afternoon</font><font color="#6a5acd">\n</font><font color="#ff00ff">&#8220;</font> <font color="#804040"><strong>if</strong></font>(<font color="#008080">$time</font> &gt; <font color="#ff00ff">12</font>);</p>
<h2 class="western">&#8220;else&#8221; and &#8220;elsif&#8221; statements</h2>
<p>Similarly, you can define an &#8220;else&#8221; block of instructions,which will be executed if the condition is not met:</p>
<p style="background-color: #dddddd"> <font color="#804040"><strong>print</strong></font><font color="#ff00ff">&#8220;</font><font color="#ff00ff">time is </font><font color="#008080">$time</font><font color="#6a5acd">\n</font><font color="#ff00ff">&#8220;</font>;<br />
<font color="#804040"><strong>if</strong></font> (<font color="#008080">$time</font> &gt; <font color="#ff00ff">12</font>) {<br />
<font color="#804040"><strong>print</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">GoodAfternoon</font><font color="#6a5acd">\n</font><font color="#ff00ff">&#8220;</font>;<br />
}<br />
<font color="#804040"><strong>else</strong></font> {<br />
<font color="#804040"><strong>print</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">Good Morning</font><font color="#6a5acd">\n</font><font color="#ff00ff">&#8220;</font>;<br />
}</p>
<p><font face="Courier 10 Pitch">Sometime  you may want to do something depending on more than one condition. Of  course you can always define If statement within others, but you can also  use elsif,as a contraction of ?else, if (something else..)?:</font></p>
<p style="background-color: #dddddd"> <font color="#804040"><strong>if</strong></font> (<font color="#008080">$time</font> &lt; <font color="#ff00ff">12</font>) {<br />
<font color="#804040"><strong>print</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">GoodMorning</font><font color="#6a5acd">\n</font><font color="#ff00ff">&#8220;</font>;<br />
}<br />
<font color="#804040"><strong>elsif</strong></font> (<font color="#008080">$time</font> &lt; <font color="#ff00ff">20</font>) {<br />
<font color="#804040"><strong>print</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">Good Afternoon</font><font color="#6a5acd">\n</font><font color="#ff00ff">&#8220;</font>;<br />
}<br />
<font color="#804040"><strong>elsif</strong></font> (<font color="#008080">$time</font> &lt; <font color="#ff00ff">23</font>) {<br />
<font color="#804040"><strong>print</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">Good Evening</font><font color="#6a5acd">\n</font><font color="#ff00ff">&#8220;</font>;<br />
}<br />
<font color="#804040"><strong>else</strong></font> {<br />
<font color="#804040"><strong>print</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">Good night</font><font color="#6a5acd">\n</font><font color="#ff00ff">&#8220;</font>;<br />
}</p>
<h2 class="western">&#8220;unless&#8221; statements</h2>
<p><font face="Courier 10 Pitch">In  Perl, you can use the <code>unless</code> statement. It behaves exactly like the <code>If</code>  statement but executes the code if the condition is not met. For instance  the following code prints <code>Good night</code> if it&#8217;s more than 11pm: </font></p>
<p style="background-color: #dddddd"> <font color="#804040"><strong>print</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">Good night</font><font color="#6a5acd">\n</font><font color="#ff00ff">&#8220;</font> <font color="#804040"><strong>unless</strong></font>(<font color="#008080">$time</font> &lt; 11pm);</p>
<h1 class="western">Loops in Perl</h1>
<p>There are many different kinds of loop in Perl. Here are the most common types.</p>
<h2 class="western">&#8220;while&#8221; loop</h2>
<p>This loop executes a block of code while a condition remains true.For instance, the following code prints <code>Hello</code> three times:</p>
<p style="background-color: #dddddd"> <font color="#008080">$i</font> = <font color="#ff00ff">0</font>;<br />
<font color="#804040"><strong>while</strong></font> (<font color="#008080">$i</font> &lt; <font color="#ff00ff">3</font>) {<br />
<font color="#804040"><strong>print</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">Hello</font><font color="#6a5acd">\n</font><font color="#ff00ff">&#8220;</font>;<br />
<font color="#008080">$i</font> = <font color="#008080">$i</font> + <font color="#ff00ff">1</font>;<br />
}</p>
<h2 class="western">&#8220;until&#8221; loop</h2>
<p>This loop executes a block of code until a condition becomes true.For instance, the following code prints <code>Hello</code> three times:</p>
<p style="background-color: #dddddd"> <font color="#008080">$i</font> = <font color="#ff00ff">1</font>;<br />
<font color="#804040"><strong>until</strong></font> (<font color="#008080">$i</font> &gt; <font color="#ff00ff">3</font>) {<br />
<font color="#804040"><strong>print</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">Hello</font><font color="#6a5acd">\n</font><font color="#ff00ff">&#8220;</font>;<br />
<font color="#008080">$i</font> = <font color="#008080">$i</font> + <font color="#ff00ff">1</font>;<br />
}</p>
<h2 class="western">&#8220;for&#8221; loop</h2>
<p>The for loop is composed of four elements:</p>
<ul>
<li>A starting assignment</li>
<li>A condition</li>
<li>An incrementation</li>
<li>A set of instructions</li>
</ul>
<p>The notation of the For-loop is as follows:</p>
<p style="font-family: monospace">for (starting assignment; condition; incrementation) {<br />
instructions<br />
}</p>
<p>The  for loop starts by executing the starting assignment. Then,while the  condition is met, it keeps executing the instructions and the  incrementation.  For instance the following For-loop prints <code>Hello</code> three  times:</p>
<p style="background-color: #dddddd"> <font color="#804040"><strong>for</strong></font> (<font color="#008080">$i</font> = <font color="#ff00ff">0</font>; <font color="#008080">$i</font> &lt; <font color="#ff00ff">3</font>; <font color="#008080">$i</font> = <font color="#008080">$i</font> + <font color="#ff00ff">1</font>) {<br />
<font color="#804040"><strong>print</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">Hello</font><font color="#6a5acd">\n</font><font color="#ff00ff">&#8220;</font>;<br />
}</p>
<p>Note that the ++ operator is usually used to increment a variable,so we could have written <span style="font-family: monospace">$i++</span> instead of <span style="font-family: monospace">$i = $i + 1;</span></p>
<p>In  Perl, you can also use the For-loop with a ?range operator?.The  notation for the range operator is ?..?. For instance, the following code  prints ?Hello? three times:</p>
<p style="background-color: #dddddd"> <font color="#804040"><strong>for</strong></font> (<font color="#ff00ff">1</font>..<font color="#ff00ff">3</font>) {<br />
<font color="#804040"><strong>print</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">Hello</font><font color="#6a5acd">\n</font><font color="#ff00ff">&#8220;</font>;<br />
}</p>
<h2 class="western">&#8220;foreach&#8221; loop</h2>
<p>The  <code>foreach</code> loop is used to iterate through the elements of an array. For  instance, the following code prints the days of the week:</p>
<p style="background-color: #dddddd"> <font color="#008080">@days</font> = (<font color="#ff00ff">&#8220;</font><font color="#ff00ff">Monday</font><font color="#ff00ff">&#8220;</font>, <font color="#ff00ff">&#8220;</font><font color="#ff00ff">Tuesday</font><font color="#ff00ff">&#8220;</font>, <font color="#ff00ff">&#8220;</font><font color="#ff00ff">Wednesday</font><font color="#ff00ff">&#8220;</font>, <font color="#ff00ff">&#8220;</font><font color="#ff00ff">Thursday</font><font color="#ff00ff">&#8220;</font>, <font color="#ff00ff">&#8220;</font><font color="#ff00ff">Friday</font><font color="#ff00ff">&#8220;</font>, <font color="#ff00ff">&#8220;</font><font color="#ff00ff">Saturday</font><font color="#ff00ff">&#8220;</font>, <font color="#ff00ff">&#8220;</font><font color="#ff00ff">Sunday</font><font color="#ff00ff">&#8220;</font>);<br />
<font color="#804040"><strong>foreach</strong></font> <font color="#008080">$dayOfTheWeek</font> (<font color="#008080">@days</font>) {<br />
<font color="#804040"><strong>print</strong></font> <font color="#ff00ff">&#8220;</font><font color="#008080">$dayOfTheWeek</font><font color="#6a5acd">\n</font><font color="#ff00ff">&#8220;</font>;<br />
}</p>
<p>You  can also use the foreach loop to iterate through the keys of  an hashtable. For instance, the following code prints the days of  the week:</p>
<p style="background-color: #dddddd"> <font color="#008080">%days</font> = ();<br />
<font color="#008080">$days</font>{<font color="#ff00ff">&#8220;</font><font color="#ff00ff">Monday</font><font color="#ff00ff">&#8220;</font>} = <font color="#ff00ff">1</font>;<br />
<font color="#008080">$days</font>{<font color="#ff00ff">&#8220;</font><font color="#ff00ff">Tuesday</font><font color="#ff00ff">&#8220;</font>} = <font color="#ff00ff">2</font>;<br />
<font color="#008080">$days</font>{<font color="#ff00ff">&#8220;</font><font color="#ff00ff">Wednesday</font><font color="#ff00ff">&#8220;</font>} = <font color="#ff00ff">3</font>;<br />
<font color="#008080">$days</font>{<font color="#ff00ff">&#8220;</font><font color="#ff00ff">Thursday</font><font color="#ff00ff">&#8220;</font>} = <font color="#ff00ff">4</font>;<br />
<font color="#008080">$days</font>{<font color="#ff00ff">&#8220;</font><font color="#ff00ff">Friday</font><font color="#ff00ff">&#8220;</font>} = <font color="#ff00ff">5</font>;<br />
<font color="#008080">$days</font>{<font color="#ff00ff">&#8220;</font><font color="#ff00ff">Saturday</font><font color="#ff00ff">&#8220;</font>} = <font color="#ff00ff">6</font>;<br />
<font color="#008080">$days</font>{<font color="#ff00ff">&#8220;</font><font color="#ff00ff">Sunday</font><font color="#ff00ff">&#8220;</font>} = <font color="#ff00ff">7</font>;<br />
<font color="#804040"><strong>foreach</strong></font> <font color="#008080">$day</font> (<font color="#804040"><strong>keys</strong></font><br />
<font color="#008080">%days</font>) {<br />
<font color="#804040"><strong>print</strong></font> <font color="#ff00ff">&#8220;</font><font color="#008080">$day</font><font color="#ff00ff"> is the day number </font><font color="#008080">$days</font><font color="#ff00ff">{</font><font color="#008080">$day</font><font color="#ff00ff">} of the week</font><font color="#6a5acd">\n</font><font color="#ff00ff">&#8220;</font>;<br />
}</p>
<h1 class="western">File Handling</h1>
<p>The  Perl language was designed to make file handling easy and efficient, so  you&#8217;ll probably won&#8217;t have any problem opening files and reading them.</p>
<h2 class="western">Opening and closing files</h2>
<p>In  order to open a file, you&#8217;ll use the ?open? instruction which takes two  arguments: the name of a file handle and the name of the file itself. The  file handle is like a variable which represents the handling of the file  within the script. For instance, in the following code we&#8217;re opening the  file ?clients.txt? with a filehandle called CLIENTS:</p>
<p style="background-color: #dddddd"> <font color="#804040"><strong>open</strong></font> (<font color="#008080">CLIENTS</font>, <font color="#ff00ff">&#8220;</font><font color="#ff00ff">clients.txt</font><font color="#ff00ff">&#8220;</font>);</p>
<p>By  default, the file is open in read-mode, which means you can only read  from it. You can decide to open a file in write-mode, in order to be able  to write into it. If the file already existed or had some data written  into it, the data will be lost. To open a file in write-mode simply add a &#8220;&gt;&#8221; in front of the file name:</p>
<p style="background-color: #dddddd"> <font color="#804040"><strong>open</strong></font> (<font color="#008080">CLIENTS</font>, <font color="#ff00ff">&#8220;</font><font color="#ff00ff">&gt;clients.txt</font><font color="#ff00ff">&#8220;</font>);</p>
<p>If  you&#8217;d rather keep the data written in the file, you can open the file in  append-mode. This way, the data will be kept, and what you&#8217;ll write in  the file will be appended to it. In order to do this add a &#8220;&gt;&gt;&#8221; in  front of the file name:</p>
<p style="background-color: #dddddd"> <font color="#804040"><strong>open</strong></font> (<font color="#008080">CLIENTS</font>, <font color="#ff00ff">&#8220;</font><font color="#ff00ff">&gt;&gt;clients.txt</font><font color="#ff00ff">&#8220;</font>);</p>
<p>The &#8220;open&#8221; instruction returns true if it managed to open the file, false  otherwise. You can use this value in order to test the success of the  operation. For instance, the following code opens the file in write-mode  and prints &#8220;Insufficient privileges&#8221; if the script doesn&#8217;t manage to do  so.</p>
<p style="background-color: #dddddd"> <font color="#804040"><strong>open</strong></font> (<font color="#008080">CLIENTS</font>, <font color="#ff00ff">&#8220;</font><font color="#ff00ff">&gt;clients.txt</font><font color="#ff00ff">&#8220;</font>) <font color="#804040"><strong>or</strong></font> <font color="#804040"><strong>print</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">Insufficientprivileges</font><font color="#6a5acd">\n</font><font color="#ff00ff">&#8220;</font>;</p>
<p>Remember  to always close the files once you&#8217;re finished with them.If you don&#8217;t  your changes might be lost. In order to close a file,simply use the &#8220;close&#8221; instruction on the filehandle:</p>
<p style="background-color: #dddddd"> <font color="#804040"><strong>close</strong></font> (<font color="#008080">CLIENTS</font>);</p>
<h2 class="western">Writing into files</h2>
<p>Once  the file is open in write mode you can write into it simply by writing  into its filehandle. The ?print? instruction writes things on the screen  by default, but you can specify a filehandle to write into. For instance,  the following code adds a line &#8220;Mr John Doe&#8221; in the end of the &#8220;clients.txt&#8221; file:</p>
<p style="background-color: #dddddd"> <font color="#804040"><strong>open</strong></font> (<font color="#008080">CLIENTS</font>, <font color="#ff00ff">&#8220;</font><font color="#ff00ff">&gt;&gt;clients.txt</font><font color="#ff00ff">&#8220;</font>) <font color="#804040"><strong>or</strong></font> <font color="#804040"><strong>die</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">Insufficientprivileges</font><font color="#6a5acd">\n</font><font color="#ff00ff">&#8220;</font>;<br />
<font color="#804040"><strong>print</strong></font> <font color="#008080">CLIENTS</font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">Mr John Doe</font><font color="#6a5acd">\n</font><font color="#ff00ff">&#8220;</font>;<br />
<font color="#804040"><strong>close</strong></font> (<font color="#008080">CLIENTS</font>);</p>
<h2 class="western">Reading data from files</h2>
<p>There are many ways to read the content of a file. Here are the two most common ways in Perl.</p>
<h3 class="western">Copying the content of the file into an array</h3>
<p>You  can copy the whole content of the file into an array. Each line will  then correspond to an element of the array. Here is an example:</p>
<p style="background-color: #dddddd"> <font color="#804040"><strong>open</strong></font>(<font color="#008080">CLIENTS</font>, <font color="#ff00ff">&#8220;</font><font color="#ff00ff">clients.txt</font><font color="#ff00ff">&#8220;</font>);<br />
<font color="#008080">@lines</font> = <font color="#008080">&lt;CLIENTS&gt;</font>;<br />
<font color="#804040"><strong>close</strong></font>(<font color="#008080">CLIENTS</font>);<br />
<font color="#804040"><strong>print</strong></font> <font color="#008080">@lines</font>;</p>
<h3 class="western">Looping through the filehandle</h3>
<p>Alternatively you can loop through the filehandle in a while loop by writing <code>while($line = <clients>)</clients></code> (think of it as ?while there are lines in the clients file, I&#8217;m assigning the current line to $line):</p>
<p style="background-color: #dddddd"> <font color="#804040"><strong>open</strong></font> (<font color="#008080">CLIENTS</font>, <font color="#ff00ff">&#8220;</font><font color="#ff00ff">clients.txt</font><font color="#ff00ff">&#8220;</font>);<br />
<font color="#804040"><strong>while</strong></font> (<font color="#008080">$line</font> = <font color="#008080">&lt;CLIENTS&gt;</font>) {<br />
<font color="#804040"><strong>print</strong></font> <font color="#008080">$line</font>;<br />
}<br />
<font color="#804040"><strong>close</strong></font> (<font color="#008080">CLIENTS</font>);</p>
<p>As  you can see, Perl makes it very easy to manipulate files. In the next  lesson we&#8217;ll look at how to search for a particular element within a  file, and how to handle and manipulate strings.</p>
]]></content:encoded>
			<wfw:commentRss>http://dantux.com/weblog/2010/08/21/learn-perl-in-10-easy-lessons-lesson-3/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Learn Perl in 10 easy lessons - Lesson 2</title>
		<link>http://dantux.com/weblog/2010/08/19/learn-perl-in-10-easy-lessons-lesson-2/</link>
		<comments>http://dantux.com/weblog/2010/08/19/learn-perl-in-10-easy-lessons-lesson-2/#comments</comments>
		<pubDate>Thu, 19 Aug 2010 23:29:13 +0000</pubDate>
		<dc:creator>marian</dc:creator>
		
		<category><![CDATA[General Computers]]></category>

		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://dantux.com/weblog/2010/08/19/learn-perl-in-10-easy-lessons-lesson-2/</guid>
		<description><![CDATA[In  the previous lesson we learnt what Perl was and how to use the  interpreter to run Perl scripts. We also wrote our first Perl script:  HelloWorld.pl. In this lesson we will learn how to use variables and  command line arguments. We will then use them in order to write our [...]]]></description>
			<content:encoded><![CDATA[<p>In  the previous lesson we learnt what Perl was and how to use the  interpreter to run Perl scripts. We also wrote our first Perl script:  HelloWorld.pl. In this lesson we will learn how to use variables and  command line arguments. We will then use them in order to write our  second Perl script: a calculator!</p>
<p><a href="http://dantux.com/weblog/2010/08/18/learn-perl-in-10-easy-lessons-lesson-1/">Learn Perl in 10 easy lessons - Lesson 1</a></p>
<h2>What are variables?</h2>
<p>Variables  are used in every computer language. If you are new to programming,  remember the variables used in algebra equations. For instance, consider  the following equation:</p>
<pre>a + 5 = 7</pre>
<p>In this equation,  there is only one variable. Its name is &#8220;a&#8221; and its value is &#8220;2&#8243;.  Variables always have a name and they always have a value. In algebra  their value is usually a number. In programming, their value can be a  number, a character, a string (which means a sequence of characters), or  even a complex object such as an array, a hashtable, a data structure,  etc&#8230; Throughout the lessons we will use variables to store many  different types of objects or values and you will become more familiar  with the different data types that a variable can represent.</p>
<h1>Variables in Perl</h1>
<p>In Perl, there a three types of variables:     * scalar variables,     * arrays,     * hashtables.</p>
<p>Scalar variables store a single value. In Perl, scalar variables names are always prefixed with a dollar sign. For instance:</p>
<pre>$a = 2;</pre>
<p>$message = &#8220;Hello World!&#8221;;Arrays can store many values. In Perl,  arrays are always prefixed with an at-sign. For instance:</p>
<pre>@colors = ("red", "green", "blue");</pre>
<pre>@primaryNumbers = (1, 2, 3, 5, 7);</pre>
<p>Hashtables  are a special kind of array: associative arrays. They are like arrays,  but for each of the value they store, they also store a corresponding  name or label. They consist of pairs of elements - a key and a data  value. In Perl, hashtables are always prefixed with a percent sign. For  instance:</p>
<pre>%phoneNumbers = (Alicia =&gt;  "090-64-773315", Tom =&gt; "085-153-3214", Jimmy =&gt; "085-285-4545");</pre>
<p>In this lesson we will focus on scalar variables and arrays. They are simple to use and they will help us write our calculator.</p>
<h1>What are command line arguments?</h1>
<p>In the previous lesson we first called our script by invoking the Perl interpreter:</p>
<pre>perl myScript.pl</pre>
<p>Then we saw how to make an implicit call to the interpreter so that the script could be called directly:</p>
<pre>./myScript.pl</pre>
<p>Whether  we call the interpreter implicitly or explicitly, we can give arguments  to the script. These arguments are given while calling the script, and  simply put after the script name:</p>
<pre>./myScript.pl argument1 argument2 argument3</pre>
<p>For  instance, instead of having a script which writes &#8220;Hello World!&#8221; on the  screen, and thus doesn&#8217;t need any argument, we will write a calculator  in this lesson. Our calculator will calculate the result of a simple  equation which we will give as a command line argument. For instance, if  we wanted our calculator to add 5 and 6, we would call it like this:</p>
<pre>./calculator.pl 5 + 6</pre>
<p>In this example, we gave three command line arguments:</p>
<ul>
<li>5</li>
<li>+</li>
<li>6</li>
</ul>
<p>The  Perl script will have to look at these arguments, identify the second  argument to know what operation to do with the first and third  arguments, calculate and print the result on the screen. Command line  arguments in PerlWhen the interpreter runs the Perl script it stores the  command line arguments in an array called @ARGV. Note that the Perl  language is case-sensitive, so it is important to use capital letters  here. @ARGV is an array, and like every array in Perl you can do the  following on it:</p>
<ul>
<li>Get the first element of the array by  typing $ARGV[0] (note that as this is a single value it is represented  by a scalar variable and prefixed with a dollar sign).</li>
<li>Get the second elements of the array by typing $ARGV[1]&#8230; etc.</li>
<li>Get the index of the last element in the array by typing $#ARGV.</li>
</ul>
<p>Note  that arrays always start from 0, not from 1. Therefore the first  element of an array, is element 0, not element 1. For instance element  number 12 corresponds to the 13th element of an array. This is a  convention in many programming languages. The index of the last element  in the array corresponds to the number of elements - 1.</p>
<p>In our example, we call our calculator by giving it three arguments:</p>
<pre>./calculator.pl 5 + 6</pre>
<p>Therefore  we can expect the @ARGV array to contain three elements, $#ARGV being  equal to 2 and $ARGV[0], $ARGV[1] and $ARGV[2] respectively being equal  to 5, &#8220;+&#8221; and 6.</p>
<h1>Your second Perl script, the Calculator!</h1>
<p>You  nearly know everything you need in order to code your second Perl  script and to write a nice calculator. In fact you could do it yourself  now! As we said before, there are more than one way to do something in  Perl. Try to program the calculator by yourself, and then have a look at  the solution below.</p>
<p>Create a file called calculator.pl and write the following into it:</p>
<p style="background-color: #cccccc"> <font color="#a020f0">#!/usr/bin/perl</font><br />
<font color="#008080">$nbArguments</font> = <font color="#008080">$#ARGV</font> + <font color="#ff00ff">1</font>;<br />
<font color="#804040"><strong>print</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">number of arguments:  </font><font color="#008080">$nbArguments</font><font color="#6a5acd">\n</font><font color="#ff00ff">&#8220;</font>;<br />
<font color="#804040"><strong>exit</strong></font>(<font color="#ff00ff">1</font>) <font color="#804040"><strong>unless</strong></font> <font color="#008080">$nbArguments</font> == <font color="#ff00ff">3</font>;<br />
<font color="#008080">$a</font> = <font color="#008080">$ARGV</font>[<font color="#ff00ff">0</font>];<br />
<font color="#008080">$b</font> = <font color="#008080">$ARGV</font>[<font color="#ff00ff">2</font>];<br />
<font color="#008080">$operation</font> = <font color="#008080">$ARGV</font>[<font color="#ff00ff">1</font>];<br />
<font color="#804040"><strong>if</strong></font> (<font color="#008080">$operation</font> <font color="#804040"><strong>eq</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">+</font><font color="#ff00ff">&#8220;</font>) {<br />
<font color="#008080">$result</font> = <font color="#008080">$a</font> + <font color="#008080">$b</font>;<br />
}<font color="#804040"><strong>elsif</strong></font> (<font color="#008080">$operation</font> <font color="#804040"><strong>eq</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">-</font><font color="#ff00ff">&#8220;</font>){<br />
<font color="#008080">$result</font> = <font color="#008080">$a</font> - <font color="#008080">$b</font>;<br />
}<font color="#804040"><strong>elsif</strong></font> (<font color="#008080">$operation</font> <font color="#804040"><strong>eq</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">/</font><font color="#ff00ff">&#8220;</font>) {<br />
<font color="#008080">$result</font> = <font color="#008080">$a</font> / <font color="#008080">$b</font>;<br />
}<font color="#804040"><strong>elsif</strong></font> (<font color="#008080">$operation</font> <font color="#804040"><strong>eq</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">x</font><font color="#ff00ff">&#8220;</font>) {<br />
<font color="#008080">$result</font> = <font color="#008080">$a</font> * <font color="#008080">$b</font>;<br />
}<br />
<font color="#804040"><strong>print</strong></font> <font color="#ff00ff">&#8220;</font><font color="#008080">$a</font><font color="#ff00ff"> </font><font color="#008080">$operation</font><font color="#ff00ff"> </font><font color="#008080">$b</font><font color="#ff00ff"> = </font><font color="#008080">$result</font><font color="#6a5acd">\n</font><font color="#ff00ff">&#8220;</font>;</p>
<p>Make the script executable:</p>
<pre>chmod a+rx calculator.pl</pre>
<p>Run the script :</p>
<pre>./calculator.pl 5 + 6</pre>
<pre>./calculator.pl 11 - 2</pre>
<pre>./calculator.pl 4 x 3</pre>
<pre>./calculator.pl 33 / 3</pre>
<p>The  script works as expected, but there probably are a lot of instructions  you didn&#8217;t fully understand within this script. So let&#8217;s look at them.</p>
<p>The first line tells where to find the interpreter, so that we can call the script directly.</p>
<p>The  second line takes the index of the last element from the @ARGV array  and adds 1 to it. That way it gets the number of command line arguments  given to the script and stores it in a variable called $nbArguments.</p>
<p>The third instruction simply prints the number of arguments on the screen.</p>
<p>There is a lot to say about the fourth instruction (<code>exit(1) unless $nbArguments == 3;</code>):</p>
<ul>
<li>&#8220;exit&#8221;  is a Perl function which makes the script stop and return a given code  to the shell. In this example the script stops and returns 1 to the  shell. 1 is a Unix convention which means that there was an error.</li>
<li>&#8220;unless&#8221;  is the opposite of &#8220;if&#8221;. These are Perl statements. In this example,  &#8220;exit&#8221; is called unless the following statement is true &#8220;$nbArguments ==  3&#8243;.</li>
<li>In Perl, and in many programming languages, the equal  sign is used to affect values to variables. For instance, when we wrote  $nbArguments = $#ARGV + 1, we assigned a value to $nbArguments. The  double equal sign &#8220;==&#8221; is a comparison operator which returns true or  false depending on the fact that the variables or values on both side  are equal or not. Perl also provides another operator &#8220;eq&#8221; which  compares strings. Try to use &#8220;eq&#8221; for string comparisons and &#8220;==&#8221; for  numbers. Remember that the &#8220;=&#8221; sign assigns values and is not a  comparison operator.</li>
<li>In brief, (<code>exit(1) unless $nbArguments == 3;</code>) means that the script will stop unless three command line arguments were given.</li>
</ul>
<p>The  three next instructions simply assign the three command line arguments  to variables. We stored the first one in $a, the third one in $b and the  second on in $operation.</p>
<p>Then, depending on the value of  $operation, we made a different calculation with $a and $b and stored  the result in a variable called $result. Note that we used &#8220;if&#8221; and  &#8220;elsif&#8221; statements. We will come back to these statements later, but for  the moment remember that &#8220;elsif&#8221; is a contraction for &#8220;else if&#8230;&#8221;.  Also, note that we used brackets to clearly identify blocks of code  between the if statements and that we indented our code. In general,  unless you have a very simple one liner to write, use brackets for each  if statement and always indent your scripts.</p>
<p>The last instruction writes a summary of the operation and its operands as well as the result on the screen.</p>
<p>In  this lesson we learnt how to use variables and command line arguments.  We also had a quick look at If statements, comparisons operator and a  few operations. In the next lesson, we&#8217;ll manipulate text files and go a  bit further in using operators and other statements.</p>
<p><a href="http://dantux.com/weblog/2010/08/21/learn-perl-in-10-easy-lessons-lesson-3/">Learn Perl in 10 easy lessons - Lesson 3</a></p>
]]></content:encoded>
			<wfw:commentRss>http://dantux.com/weblog/2010/08/19/learn-perl-in-10-easy-lessons-lesson-2/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Learn Perl in 10 easy lessons - Lesson 1</title>
		<link>http://dantux.com/weblog/2010/08/18/learn-perl-in-10-easy-lessons-lesson-1/</link>
		<comments>http://dantux.com/weblog/2010/08/18/learn-perl-in-10-easy-lessons-lesson-1/#comments</comments>
		<pubDate>Wed, 18 Aug 2010 16:01:16 +0000</pubDate>
		<dc:creator>marian</dc:creator>
		
		<category><![CDATA[General Computers]]></category>

		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://dantux.com/weblog/2010/08/18/learn-perl-in-10-easy-lessons-lesson-1/</guid>
		<description><![CDATA[This tutorial was reproduced from this source:
http://www.linuxforums.org/articles/learn-perl-in-10-easy-lessons-lesson-1_120.html
The reason I reproduced it here, is because that website had issues with displaying code and often the examples would fail due to missing characters.
 Thanks  to the Perl scripting language you can automate various tasks in your  Linux system. Learning Perl is both easy and fun, [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial was reproduced from this source:</p>
<p><a href="http://www.linuxforums.org/articles/learn-perl-in-10-easy-lessons-lesson-1_120.html" title="Learn Perl in 10 Easy Lesons - Leson 1" target="_blank">http://www.linuxforums.org/articles/learn-perl-in-10-easy-lessons-lesson-1_120.html</a></p>
<p>The reason I reproduced it here, is because that website had issues with displaying code and often the examples would fail due to missing characters.</p>
<hr /> Thanks  to the Perl scripting language you can automate various tasks in your  Linux system. Learning Perl is both easy and fun, and you&#8217;ll soon be  able to write scripts which will make your life easier. In these series  of articles I&#8217;ll start by explaining the basics and I&#8217;ll progressively  introduce more complex concepts and advanced techniques.<br />
I&#8217;ll try to explain things as much as possible, so whether you are  familiar to programming or not, you should find it easy to learn Perl  and be comfortable with using this language after the end of this series  of articles.</p>
<h2>History of Perl</h2>
<p>Larry Wall created a scripting language  in 1987, which he called the &#8220;Practical Extraction and Report Language&#8221;.  It was designed as a text-processing language for Unix operating  systems. Various tools and languages already existed (Unix shells, sed,  awk, C&#8230;etc) and programmers usually used many of them together. Larry  Wall wanted the language to cover all aspects and needs related to  text-processing so the programmer wouldn&#8217;t have to use any other tool in  conjunction with Perl. Also, he designed Perl as a language that was  easy and fast to use but which would also allow the programmer to get  into the innards of things.</p>
<p>Perl offered features no other  language had offered before. It was filling a niche no other tool had,  and it immediately became popular. In 1994, Perl 5 was released and at  that stage it was a stable and well established general purpose  programming language.</p>
<p>Particularities of PerlPerl is truly unique.  As we go along and study its different features you&#8217;ll probably see  that by yourself. Larry Wall applied a lot of his linguistic knowledge  into the making of Perl. Some people even consider it a natural  language. Its vocabulary is extremely rich and its grammar is very  flexible. Perl programmers usually say &#8220;There&#8217;s more than one way to do  it&#8221;. In fact, you can literally write a Perl script your way, with your  own style. Some people even do &#8220;poetry&#8221; in Perl <img src='http://dantux.com/weblog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> Because of this, some  Perl scripts can be very difficult to read. Writing them though, is  always a pleasure.</p>
<p>The Perl interpreterPerl is an interpreted  language. This means that the code you will write using the Perl  language will need a program called the Perl interpreter in order to be  run. For instance, if you write a Perl script in a file called  <code>myScript.pl</code> (.pl is commonly used as an extension for Perl scripts), you  will not be able to run it directly. You will need to call the  interpreter to run it:</p>
<p><code>perl myScript.pl</code></p>
<p>In this example <code>myScript.pl</code> is your Perl script and <code>perl</code> is the Perl interpreter. Installation of the Perl interpreterThe  Perl interpreter is an essential tool and it is usually installed by  default on most GNU/Linux distributions. For instance, the following  distributions come with a recent and updated version of Perl:</p>
<ul>
<li>Suse 10.1</li>
<li>Fedora Core 5</li>
<li>Debian Testing</li>
<li>Ubuntu 5.10</li>
<li>Mandriva 2006</li>
<li>Slackware 10.2</li>
<li>Mepis 3.4-3</li>
<li>Gentoo 2006.0</li>
<li>Knoppix 5.0</li>
</ul>
<p>For an exhaustive list of distributions which include perl, you can search distrowatch.com: <a href="http://distrowatch.com/search.php?pkg=perl&amp;pkgver=5.8.8#pkgsearch" target="_blank" rel="nofollow">http://distrowatch.com/search.php?pkg=perl&amp;pkgver=5.8.8#pkgsearch</a></p>
<p>To make sure the interpreter is installed on your machine and to see which version it is, you can open a terminal and type:</p>
<p><code>perl -v</code></p>
<p>If it is installed, this should print the version of Perl installed on your machine:</p>
<blockquote><p><em>marian@dantux.com [~]# perl -v</em></p>
<p><em>This is perl, v5.8.8 built for x86_64-linux-thread-multi</em></p>
<p><em>Copyright 1987-2006, Larry Wall</em></p>
<p><em>Perl may be copied only under the terms of either the Artistic License or the<br />
GNU General Public License, which may be found in the Perl 5 source kit.</em></p>
<p><em>Complete documentation for Perl, including FAQ lists, should be found on<br />
this system using &#8220;man perl&#8221; or &#8220;perldoc perl&#8221;.  If you have access to the<br />
Internet, point your browser at http://www.perl.org/, the Perl Home Page.</em></p></blockquote>
<p><span>If  Perl is not installed on your system, you will certainly be able to  install it through your distribution&#8217;s package manager. Simply search  for perl within your distribution&#8217;s repositories, or start considering  using another distribution. After all, Perl is an essential tool which  should be included by default and regularly updated by your  distribution: <a href="http://www.cpan.org/ports/#linux" class="smarterwiki-linkify">http://www.cpan.org/ports/#linux</a> </span></p>
<p>Implicit use of the interpreterThe  Perl interpreter is usually used to run Perl scripts which are written  in a file. It also features an interactive mode which you can use by  simply typing perl without any argument. However, in this lesson we will  focus on using files.</p>
<p>In order to run a Perl script, you can call the interpreter with the file as an argument:<br />
<code>perl myScript.pl</code><br />
&#8230;  or you can tell the Perl script where to find the interpreter and make  the script executable. This is common practice among programmers and you  are encouraged to do so. Within the script file, the first line tells  the shell how to interpret the file. This line basically shows the path  to the Perl interpreter:<br />
<font color="#a020f0">#!/usr/bin/perl</font><br />
Note:  The Perl interpreter is usually installed in /usr/bin, but your system  might be different. To make sure, type &#8220;which perl&#8221;:<br />
<code>clem@pluto:~&gt; which perl/usr/bin/perl</code><br />
Also make sure your Perl scripts are executable and have proper permissions:<br />
<code>chmod a+rx myScript.pl</code><br />
Once  the script is executable, it can be run directly. The shell looks at  the first line of the file which starts with #!, and then it runs the  interpreter which path is found on that line with the rest of the file.  In other words, thanks to that little trick, you can run you Perl script  directly:</p>
<p class="snippet">./myScript.pl</p>
<p>Although  you are not explicitly calling the interpreter here, keep in mind that  it is actually run by the shell on your behalf and that it is the  interpreter which runs your script.</p>
<p>Your very first Perl script!You  have your interpreter installed, and you&#8217;re ready for your first  script: a simple script that writes &#8220;Hello World!&#8221; on the screen (I  know.. it&#8217;s a bit useless, but it&#8217;s more or less a tradition when you&#8217;re  learning a new programming language, so let&#8217;s simply write &#8220;Hello  World&#8221; and enjoy the lesson since it&#8217;s still easy to understand :)).</p>
<p>Create a file called <code>helloWorld.pl</code> and write the following into it:</p>
<p style="background-color: #dddddd"> <font face="monospace"><br />
<font color="#a020f0">#!/usr/bin/perl</font><br />
<font color="#804040"><strong>print</strong></font> <font color="#ff00ff">&#8220;</font><font color="#ff00ff">Hello World!</font><font color="#6a5acd">\n</font><font color="#ff00ff">&#8220;</font> </font></p>
<p>Make the script executable:</p>
<p>Run the script:<br />
<code>./helloWorld.pl</code></p>
<p>As  you probably expected, &#8220;Hello World!&#8221; gets printed on the screen. The  script only contains two lines and is quite easy to understand. The  first line will always be the same; it tells your shell where to find  the Perl interpreter. The second line is actually the only Perl  instruction of your script, it tells the interpreter to print &#8220;Hello  World!&#8221; on the screen. In Perl, each instruction finishes with a  semicolon. If you&#8217;re new to programming you&#8217;ll probably forget  semicolons in your code, so be careful with that. The reason for the  semicolon is that an instruction can sometimes be long and take more  than one line, so it is an effective way to mark the end of an  instruction. Most languages use semicolons and once you&#8217;re used to it,  it seems very natural. You might also wonder what the &#8220;n&#8221; is for. It  simply is a special character which corresponds to the new line  character (as when someone presses the Enter key) so that the cursor  goes the next line of the screen after printing &#8220;Hello World!&#8221;.</p>
<p>In  the next lesson we&#8217;ll start using variables, opening files and do a lot  of things which will come handy for you later. Now that you know what  Perl is and how to use it, we&#8217;ll start to focus on the language itself.</p>
<p><a href="http://dantux.com/weblog/2010/08/19/learn-perl-in-10-easy-lessons-lesson-2/">Learn Perl in 10 easy lessons - Lesson 2</a></p>
]]></content:encoded>
			<wfw:commentRss>http://dantux.com/weblog/2010/08/18/learn-perl-in-10-easy-lessons-lesson-1/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How to enable the showing of full path in Finder window</title>
		<link>http://dantux.com/weblog/2010/01/23/how-to-enable-the-showing-of-full-path-in-finder-window/</link>
		<comments>http://dantux.com/weblog/2010/01/23/how-to-enable-the-showing-of-full-path-in-finder-window/#comments</comments>
		<pubDate>Sat, 23 Jan 2010 03:50:44 +0000</pubDate>
		<dc:creator>marian</dc:creator>
		
		<category><![CDATA[MAC]]></category>

		<guid isPermaLink="false">http://dantux.com/weblog/2010/01/23/how-to-enable-the-showing-of-full-path-in-finder-window/</guid>
		<description><![CDATA[Just run this in Terminal:
$ defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES
Remember to restart Finder. You can do it with the killall command:
$ killall Finder
]]></description>
			<content:encoded><![CDATA[<p>Just run this in Terminal:</p>
<p>$ <code>defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES</code></p>
<p>Remember to restart Finder. You can do it with the killall command:</p>
<p>$ <code>killall Finder</code></p>
]]></content:encoded>
			<wfw:commentRss>http://dantux.com/weblog/2010/01/23/how-to-enable-the-showing-of-full-path-in-finder-window/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>

