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

<channel>
	<title>killianfaughnan.com &#187; perl</title>
	<atom:link href="http://killianfaughnan.com/tag/perl/feed/" rel="self" type="application/rss+xml" />
	<link>http://killianfaughnan.com</link>
	<description>Just another WordPress site</description>
	<lastBuildDate>Wed, 23 Feb 2011 09:05:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Writing Better Perl</title>
		<link>http://killianfaughnan.com/2011/02/23/writing-better-perl/</link>
		<comments>http://killianfaughnan.com/2011/02/23/writing-better-perl/#comments</comments>
		<pubDate>Wed, 23 Feb 2011 09:05:06 +0000</pubDate>
		<dc:creator>kfaughnan</dc:creator>
				<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://www.killianfaughnan.com/?p=264</guid>
		<description><![CDATA[I&#8217;ve recently started using a few tools to help clean up my Perl.  The first of these tools I use for standardised formatting of my Perl scripts.  The second I use to analyse my Perl source code. Perltidy The first of these tools is called perltidy, which is a perl script &#8220;indenter and reformatter&#8221;.  Essentially [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently started using a few tools to help clean up my Perl.  The first of these tools I use for standardised formatting of my Perl scripts.  The second I use to analyse my Perl source code.</p>
<h2>Perltidy</h2>
<p>The first of these tools is called perltidy, which is a perl script &#8220;indenter and reformatter&#8221;.  Essentially it allows you to keep a universal formatting standard across all your Perl scripts, and even better, across all Perl scripts written by others you work with too if you can convince them of the need!</p>
<p>Perltidy has many options, but I tend to stick to a pretty basic set of requirements:</p>
<pre>perltidy -ce -b &lt;file&gt;.pl</pre>
<p>The -ce flags above are short for &#8211;cuddled-else, which means we want to have else and elsif&#8217;s following immediately after the curly brace belonging to the previous block.</p>
<p>The -b tells perltidy to create a backup of the file as &lt;file&gt;.pl.bak, and modify the file in place.  The default behaviour is to writeout a new file to &lt;file&gt;.pl.tdy.</p>
<h2>Perlcritic</h2>
<p>For analysing my perl I use <em>perlcritic</em>.  The following extract is taken from the perlcritic man page:</p>
<p>&#8220;the executable front-end to the Perl::Critic engine, which attempts to identify awkward, hard to read, error-prone, or unconventional constructs in your code.  Most of the rules are based on Damian Conway&#8217;s book Perl Best Practices.&#8221;</p>
]]></content:encoded>
			<wfw:commentRss>http://killianfaughnan.com/2011/02/23/writing-better-perl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Renaming Files Using Perl</title>
		<link>http://killianfaughnan.com/2009/06/16/renaming-files-using-perl/</link>
		<comments>http://killianfaughnan.com/2009/06/16/renaming-files-using-perl/#comments</comments>
		<pubDate>Tue, 16 Jun 2009 13:30:07 +0000</pubDate>
		<dc:creator>kfaughnan</dc:creator>
				<category><![CDATA[perl]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[regexp]]></category>
		<category><![CDATA[regular expression]]></category>
		<category><![CDATA[rename]]></category>

		<guid isPermaLink="false">http://killianfaughnan.com/?p=188</guid>
		<description><![CDATA[I finally got around to writing a script to rename all my music files to the same convention. That being either &#60;track&#62;-&#60;song&#62;.mp3 or &#60;song&#62;.mp3 with no spaces (I prefer underscores instead) and all lowercase. I know, it looks so simple! But the problem is all those occasions where you have something like &#60;track&#62;_-_&#60;SONG&#62;_-_(&#60;remix by&#62;).mp3 which [...]]]></description>
			<content:encoded><![CDATA[<p>I finally got around to writing a script to rename all my music files to the same convention. That being either &lt;track&gt;-&lt;song&gt;.mp3 or &lt;song&gt;.mp3  with no spaces (I prefer underscores instead) and all lowercase. I know, it looks so simple! But the problem is all those occasions where you have something like &lt;track&gt;_-_&lt;SONG&gt;_-_(&lt;remix by&gt;).mp3 which just looks horrible! What this script does could be done in a lot less lines than I have here, but I thought it might be a good way for someone who is unsure of regular expressions to get a grasp on the topic.</p>
<p>So heres the script:</p>
<pre>#!/usr/bin/perl -w
use strict;

# Open 'find' process to list files recursively with paths
open(FIND, "find |");
while(&lt;FIND&gt;) {
 # remove leading / trailing whitespace
 chomp; 

 # Don't rename ourself
 next if $_ eq $0;       

 # create temp file (windows wont allow to rename in place from uppercase to lowercase)
 my $name = $_;
 my $tmp = $_.'~';
 rename($name, $tmp);

 # make lowercase
 $name = lc($name);
 rename($tmp, $name);

 my $newname = $name;
 # remove apostrophes
 $newname =~ s/[']//g;
 # remove round brackets and replace with hyphens
 $newname =~ s/[()]/-/g;
 # remove spaces and replace with underscores
 $newname =~ s/ /_/g;
 # remove where in sequence there is underscore, hyphen, underscore and replace with a hyphen
 $newname =~ s/_-/-/g;
 $newname =~ s/-_/-/g;
 # where there are one or more digits followed by an underscore change the underscore to a hyphen
 $newname =~ s/(d+)_/$1-/g;
 # remove all ampersands and replace them with '_and_'
 $newname =~ s/&amp;/_and_/g;
 # remove underscores where there are 2 or more, and replace with a single underscore
 $newname =~ s/_{2,}/_/g;

 # write out the changes
 rename($name,$newname);

}
close(FIND);</pre>
]]></content:encoded>
			<wfw:commentRss>http://killianfaughnan.com/2009/06/16/renaming-files-using-perl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Renaming Files As Lowercase Using Perl</title>
		<link>http://killianfaughnan.com/2008/11/12/renaming-files-as-lowercase-using-perl/</link>
		<comments>http://killianfaughnan.com/2008/11/12/renaming-files-as-lowercase-using-perl/#comments</comments>
		<pubDate>Wed, 12 Nov 2008 10:37:18 +0000</pubDate>
		<dc:creator>kfaughnan</dc:creator>
				<category><![CDATA[perl]]></category>
		<category><![CDATA[find]]></category>
		<category><![CDATA[lc]]></category>
		<category><![CDATA[lowercase]]></category>
		<category><![CDATA[rename]]></category>

		<guid isPermaLink="false">http://killianfaughnan.com/?p=50</guid>
		<description><![CDATA[This is just a handy script I use for renaming my music collection. It got to be too much trouble having the first letter of each word capitalised, getting new music with the names all in caps, or all in lowercase&#8230;so I decided to make everything lowercase and write a script to do it for [...]]]></description>
			<content:encoded><![CDATA[<p>This is just a handy script I use for renaming my music collection. It got to be too much trouble having the first letter of each word capitalised, getting new music with the names all in caps, or all in lowercase&#8230;so I decided to make everything lowercase and write a script to do it for me!</p>
<pre>#!/usr/bin/perl -w
use strict;

# open 'find' process to list files recursively with paths
open(FIND, "find |");

while(&lt;FIND&gt;) {

  chomp;
  # don't rename ourself if script in same as executing
  next if $_ eq $0;

  # first move the file to $name~ and then back to the lowercase original to allow for fat32 ignoring case,
  # and therefore claiming that a file with this name already exists
  my $name = $_;
  my $tmp = $_.'~';

  rename($name, $tmp);
  rename($tmp, lc($name));
}
close(FIND);</pre>
<p>To start off the script just opens a handle on the linux <a title="Linux Find Command" href="http://www.ss64.com/bash/find.html" target="_blank">find</a> command with an output <a title="Linux Pipe" href="http://www.manpagez.com/man/2/pipe/" target="_blank">pipe</a> and calls it&#8230;well&#8230;FIND. The while loop will then iterate through each line the find command returns and rename using the <a title="Perl Rename Function" href="http://perldoc.perl.org/functions/rename.html" target="_blank">rename()</a> function.</p>
<p>The rename function takes in two arguments, the old name (as returned by find) and the new name. As the current returned value we are looking at is stored in the special variable $_, we can pass this as the old file name. We can then just use the <a title="Perl LC Function" href="http://perldoc.perl.org/functions/lc.html" target="_blank">lc()</a> function to convert the old mane to lowercase by putting $_ as the argument for lc(). Finally we close the filehandle on FIND. And voila!</p>
]]></content:encoded>
			<wfw:commentRss>http://killianfaughnan.com/2008/11/12/renaming-files-as-lowercase-using-perl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CsvSQL Man Page</title>
		<link>http://killianfaughnan.com/2008/11/12/csvsql-man-page/</link>
		<comments>http://killianfaughnan.com/2008/11/12/csvsql-man-page/#comments</comments>
		<pubDate>Wed, 12 Nov 2008 10:27:21 +0000</pubDate>
		<dc:creator>kfaughnan</dc:creator>
				<category><![CDATA[csvsql]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[man]]></category>
		<category><![CDATA[manual]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://killianfaughnan.com/?p=42</guid>
		<description><![CDATA[NAME Csvsql &#8211; use SQL queries to access information in CSV files DESCRIPTION csvsql enables you to access a CSV file as if it were a table in a database. This means you can use SQL queries, with each ’common seperated value’ as part of a column. Traditionally in order to access specific information from [...]]]></description>
			<content:encoded><![CDATA[<p>NAME<br />
Csvsql &#8211; use SQL queries to access information in CSV files</p>
<p>DESCRIPTION<br />
csvsql enables you to access a CSV file as if it were a table in a database.  This means you can use SQL queries, with each ’common seperated<br />
value’ as part of a column.</p>
<p>Traditionally in order to access specific information from a CSV file it can take considerable use of regular expressions, awk and sed amongst<br />
others. What if you only wanted to take out a handful of lines from a large file? It nearly becomes easier to do it manually than to figure out<br />
the expressions needed otherwise.</p>
<p>SYNOPSIS<br />
Csvsql [-h] [-v] [-c "command"] [-s seperator] [-p filename] [-l logging]</p>
<p>COMMAND LINE OPTIONS<br />
-h  This help message</p>
<p>-v  Prints Csvsql version number</p>
<p>-c  Run this command and exit. Command should be in &#8220;&#8221;</p>
<p>-s  User defined separator (defaults to , ) if using a space please use &#8221; &#8221;</p>
<p>-p  Copy output to a specified file. This option can also be specified in interactive mode.</p>
<p>-l  Switch logging on or off. 1 = on, 0 = off (defaults to 1)<br />
INTERACTIVE OPTIONS<br />
SUPPORTED SQL QUERIES</p>
<p>At present the supported queries are:</p>
<p>SELECT<br />
SELECT [ * | select_expr ]<br />
[ FROM file_name [ WHERE where_condition ]<br />
[ LIMIT row_count ] ]</p>
<p>INSERT<br />
INSERT INTO file_name<br />
(col_name,col_name,&#8230;)<br />
VALUES (expr,expr,&#8230;)</p>
<p>UPDATE<br />
UPDATE file_name<br />
SET col_name=expr<br />
[ WHERE where_condition ]</p>
<p>DELETE<br />
DELETE FROM file_name<br />
WHERE where_condition</p>
<p>CREATE<br />
CREATE file_name<br />
(col_name,col_name,&#8230;)<br />
WITH VALUES (expr,expr&#8230;)</p>
<p>WHERE<br />
The WHERE clause, if given, indicates the condition or conditions that rows must satisfy to be selected.  where_condition is an expression<br />
that evaluates to true for each row to be selected. The statement selects all rows if there is no WHERE clause.</p>
<p>The WHERE clause can be used for selections that include an AND or an OR operator, where AND requires for both conditions to be true, and OR<br />
requires either one or the other.</p>
<p>NON-SQL COMMANDS</p>
<p>open [file_name]<br />
Opens a file and read it’s contents into memory</p>
<p>close [file_name]<br />
Closes the specified file and release all locks. If no file specified will default to the currently opened file.</p>
<p>describe [file_name]<br />
Display a files column headings.</p>
<p>set pipe = [file_name | off]<br />
Will copy output to a specified file. This option can also be specified as a command line option</p>
<p>ls [path]<br />
List specified directory</p>
<p>dir [path]<br />
Same as above</p>
<p>clear<br />
Clear screen</p>
<p>cls Same as above</p>
<p>version<br />
Print version number</p>
<p>dump<br />
Dump a listing of data in memory</p>
<p>SPECIAL VALUES<br />
isnull<br />
In order to check for, or insert a null value, you should use isnull in place of ’’ Example: SELECT * FROM file_name WHERE col_name  = isnull</p>
<p>LICENCE<br />
Copyright (C) 2008 Killian Faughnan</p>
<p>CsvSQL is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Soft‐<br />
ware Foundation, either version 3 of the License, or (at your option) any later version.</p>
<p>You should have received a copy of the GNU General Public License along with CsvSQL. If not, see &lt;http://www.gnu.org/licenses&gt;</p>
<p>Author: Killian Faughnan &lt;killian [at] killianfaughnan [dot] com&gt;</p>
<p>&lt;http://www.killianfaughnan.com&gt;</p>
<p>SEE ALSO<br />
For further documentation please see &lt;http://docs.killianfaughnan.com/csvsqldocs&gt;</p>
]]></content:encoded>
			<wfw:commentRss>http://killianfaughnan.com/2008/11/12/csvsql-man-page/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CsvSQL</title>
		<link>http://killianfaughnan.com/2008/11/12/csvsql/</link>
		<comments>http://killianfaughnan.com/2008/11/12/csvsql/#comments</comments>
		<pubDate>Wed, 12 Nov 2008 10:24:07 +0000</pubDate>
		<dc:creator>kfaughnan</dc:creator>
				<category><![CDATA[csvsql]]></category>
		<category><![CDATA[csv]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://killianfaughnan.com/?p=39</guid>
		<description><![CDATA[About CsvSQL Csvsql is a project I started for my BSc&#8217;s final year software project. It is written in Perl and can be used to access information in csv files in the same way you would access a table in a database. This means you can use SQL queries, with each common seperated value as [...]]]></description>
			<content:encoded><![CDATA[<h3>About CsvSQL</h3>
<p>Csvsql is a project I started for my BSc&#8217;s final year software project. It is written in Perl and can be used to access information in csv files in the same way you would access a table in a database. This means you can use SQL queries, with each common seperated value as a field in a column.</p>
<p>Traditionally in order to access specific information from a csv file it can take considerable use of regular expressions, and awk commands. What if you only wanted to take out a handful of lines from a large file? It nearly becomes easier to do it manually than to figure out the expressions needed in awk.</p>
<h3>CsvSQL Download</h3>
<p>You can download CsvSQL here. If you have and questions, suggestions, criticisms or comments please let me know as I would be interested in hearing them. And if you do decide to use this program for something, let me know how you get on!</p>
<p><a href="http://killianfaughnan.com/wp-content/uploads/2008/11/csvsqltar.gz" target="_blank">download csvsql</a></p>
]]></content:encoded>
			<wfw:commentRss>http://killianfaughnan.com/2008/11/12/csvsql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Printing In Perl</title>
		<link>http://killianfaughnan.com/2008/11/12/printing-in-perl/</link>
		<comments>http://killianfaughnan.com/2008/11/12/printing-in-perl/#comments</comments>
		<pubDate>Wed, 12 Nov 2008 10:17:37 +0000</pubDate>
		<dc:creator>kfaughnan</dc:creator>
				<category><![CDATA[perl]]></category>
		<category><![CDATA[hello world]]></category>
		<category><![CDATA[print]]></category>
		<category><![CDATA[printing]]></category>
		<category><![CDATA[string]]></category>

		<guid isPermaLink="false">http://killianfaughnan.com/?p=34</guid>
		<description><![CDATA[Printing A String Printing a string in Perl couldn&#8217;t be easier, you simply use the print function. If you want to print a line in Perl you simple need to write the following: print "Hello World!"; Output: Hello World! Similarly you you print a variable as follows: my $string = "Hello World!"; print $string; Output: [...]]]></description>
			<content:encoded><![CDATA[<h3>Printing A String</h3>
<p>Printing a string in Perl couldn&#8217;t be easier, you simply use the <a title="Perl Print Function" href="http://perldoc.perl.org/functions/print.html" target="_blank">print</a> function. If you want to print a line in Perl you simple need to write the following:</p>
<pre>  print "Hello World!";</pre>
<pre>  Output: Hello World!</pre>
<p>Similarly you you print a variable as follows:</p>
<pre>  my $string = "Hello World!";
  print $string;

  Output: Hello World!</pre>
]]></content:encoded>
			<wfw:commentRss>http://killianfaughnan.com/2008/11/12/printing-in-perl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reading From A File In Perl</title>
		<link>http://killianfaughnan.com/2008/11/12/reading-from-a-file-in-perl/</link>
		<comments>http://killianfaughnan.com/2008/11/12/reading-from-a-file-in-perl/#comments</comments>
		<pubDate>Wed, 12 Nov 2008 08:47:37 +0000</pubDate>
		<dc:creator>kfaughnan</dc:creator>
				<category><![CDATA[perl]]></category>
		<category><![CDATA[files]]></category>
		<category><![CDATA[reading]]></category>

		<guid isPermaLink="false">http://killianfaughnan.com/?p=21</guid>
		<description><![CDATA[Opening a file eading from a file in Perl is pretty simple. After all, the language is pretty much built around dealing with text. The first step is to open the file, in which case you just need to use the following: open(INFILE,"&#60; myfile.txt") or die "Can't open file: $!"; In this statement we are [...]]]></description>
			<content:encoded><![CDATA[<h3>Opening a file</h3>
<p>eading from a file in Perl is pretty simple. After all, the language is pretty much built around dealing with text. The first step is to open the file, in which case you just need to use the following:</p>
<pre>  open(INFILE,"&lt; myfile.txt") or die "Can't open file: $!";</pre>
<p>In this statement we are using the <a title="Perl Open Function" href="http://perldoc.perl.org/functions/open.html" target="_blank">open()</a> function with a filehandle and filename as arguments. We will use this filehandle for all further actions on the file. The rest of the statement will print the error &#8220;Can&#8217;t open file: $!&#8221; where $! is the filename, should the script be unable to open the file.</p>
<h3>Reading an open file and printing it&#8217;s contents</h3>
<p>To read a file which already has an open filehandleyou can use the following:</p>
<pre>  while(&lt;INFILE&gt;){
    print $_;
  }</pre>
<p>This while loop will read through the file line by line, printing the contents, until it reaches the last line where it will terminate. In Perl $_ is a special character. It represents the &#8220;default input and pattern matching space&#8221;, which in the context above means that each line of the file is assigned to $_ in turn as the file is being read. So when we use print $_ we are printing the current line, and with each iteratation of the loop that line changed to the next.</p>
]]></content:encoded>
			<wfw:commentRss>http://killianfaughnan.com/2008/11/12/reading-from-a-file-in-perl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Perl Loop Basics</title>
		<link>http://killianfaughnan.com/2008/11/12/perl-loop-basics/</link>
		<comments>http://killianfaughnan.com/2008/11/12/perl-loop-basics/#comments</comments>
		<pubDate>Wed, 12 Nov 2008 08:11:28 +0000</pubDate>
		<dc:creator>kfaughnan</dc:creator>
				<category><![CDATA[perl]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[for]]></category>
		<category><![CDATA[foreach]]></category>
		<category><![CDATA[loops]]></category>

		<guid isPermaLink="false">http://killianfaughnan.com/?p=16</guid>
		<description><![CDATA[For loop The below will print &#8220;Hello!&#8221; 10 times: for ($i = 0; $i &#38;lt; 10 ; $i++){ print "Hello!n"; } Output: Hello! Foreach loop For iterating through an array it can be handy to use a foreach loop. In the example below the foreach loop will access each variables in the array and allow [...]]]></description>
			<content:encoded><![CDATA[<h3>For loop</h3>
<p>The below will print &#8220;Hello!&#8221; 10 times:</p>
<pre>  for ($i = 0; $i &amp;lt; 10 ; $i++){   print "Hello!n"; }</pre>
<pre>  Output: Hello!</pre>
<h3>Foreach loop</h3>
<p>For iterating through an array it can be handy to use a foreach loop. In the example below the foreach loop will access each variables in the array and allow you to use it as $i:</p>
<pre>  @array = ('one','two','three');</pre>
<pre>  foreach $i (@array) {   print "$i, "; }</pre>
<pre>  Output: one, two, three,</pre>
]]></content:encoded>
			<wfw:commentRss>http://killianfaughnan.com/2008/11/12/perl-loop-basics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Perl Array Basics</title>
		<link>http://killianfaughnan.com/2008/11/12/perl-array-basics/</link>
		<comments>http://killianfaughnan.com/2008/11/12/perl-array-basics/#comments</comments>
		<pubDate>Tue, 11 Nov 2008 23:40:22 +0000</pubDate>
		<dc:creator>kfaughnan</dc:creator>
				<category><![CDATA[perl]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[push]]></category>

		<guid isPermaLink="false">http://killianfaughnan.com/?p=1</guid>
		<description><![CDATA[Initialising or clearing an array To create a new array it is as simple as declaring it as shown below. This method can also be used to clear an existing array, though you will have to drop the my. my @array = (); Creating an array with predefined elements To create a new array with [...]]]></description>
			<content:encoded><![CDATA[<h3>Initialising or clearing an array</h3>
<p>To create a new array it is as simple as declaring it as shown below. This method can also be used to clear an existing array, though you will have to drop the my.</p>
<pre>my @array = ();</pre>
<h3>Creating an array with predefined elements</h3>
<p>To create a new array with elements just declare the array with a comma delimited list in brackets after it.</p>
<pre>my @array = (hendrix,santana,young,clapton);</pre>
<h3>Adding elements to the end of an array with push()</h3>
<p>In this example we will add new elements to an array using the <a title="Perl Push Function" href="http://perldoc.perl.org/functions/push.html" target="_blank">push()</a> function. To do this we will use a loop to push elements from an existing array @array into our new array cleverly named&#8230;@newarray. The variable $item represents the value of the currently accessed element in @array</p>
<pre>my @array = (hendrix,santana,young,clapton);</pre>
<pre>my @newarray = ();</pre>
<pre>foreach my $item (@array) {</pre>
<pre>  push( @newarray,$item);</pre>
<pre>}</pre>
]]></content:encoded>
			<wfw:commentRss>http://killianfaughnan.com/2008/11/12/perl-array-basics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

