<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>lesleyb</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/lesleyb/" />
    <link rel="self" type="application/atom+xml" href="http://blogs.perl.org/users/lesleyb/atom.xml" />
    <id>tag:blogs.perl.org,2009-11-03:/users/lesleyb//1473</id>
    <updated>2012-08-23T13:27:11Z</updated>
    <subtitle>A blog about the Perl programming language</subtitle>
    <generator uri="http://www.sixapart.com/movabletype/">Movable Type Pro 4.38</generator>

<entry>
    <title>Going to Perl School</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/lesleyb/2012/08/going-to-perl-school.html" />
    <id>tag:blogs.perl.org,2012:/users/lesleyb//1473.3729</id>

    <published>2012-08-23T13:21:06Z</published>
    <updated>2012-08-23T13:27:11Z</updated>

    <summary><![CDATA[[This is a repost of what I wrote over here&nbsp;]I first used Perl in 2002 - about 10 years ago.&nbsp; Over the years, I have continued to dabble in it but often felt that stuff like Mason and mod_perl were...]]></summary>
    <author>
        <name>lesleyb</name>
        <uri>http://herlug.org.uk/blog</uri>
    </author>
    
    <category term="perlschool" label="Perl School" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/lesleyb/">
        <![CDATA[<p>[This is a repost of what I wrote <a href="http://herlug.org.uk/blog/2012/08/perlschool.html">over here</a>&nbsp;]</p><p>I first used Perl in 2002 - about 10 years ago.&nbsp; Over the years, I have continued to dabble in it but often felt that stuff like Mason and mod_perl were a little beyond what I needed to achieve.&nbsp; So I stuck with a Perl/CGI format when writing web pages.&nbsp; A <a href="http://mail-archives.apache.org/mod_mbox/perl-modperl/201207.mbox/%3C464941c878d6fe096214a7a04db686be@buczak.us%3E">recent post</a> persuaded me to move forward from there - that is a work in progress.<br /><br />So, after 10 years, why go to <a href="http://perlschool.co.uk/upcoming/">Modern Perl for Non-Perl Programmers</a> held by Dave Cross @ <a href="http://perlschool.co.uk/">Perl School</a>?&nbsp; <br /><br />(I&nbsp; will confess I was a little concerned I'd be taking a place that could be used by someone else but I am glad I went.)<br /><br />Dave started with a walk through the basics of Perl, sigils, arrays, hashes and lists.&nbsp; Most of this I had encountered before, but, back when I just started Perl, I got confused between the use of (), [] and {} and what a list actually was.&nbsp; As a result, I ended up with some fairly interesting results trying to define hashes, arrays and access the elements and key value pairs within.<br /><br />I came from a C programming background; web and mail server work so I was used to strongly typed languages.&nbsp; With some languages you can have really mad hard typing; you can declare two integers on two separate lines and then get the compiler to consider them of different types - even though they are both integers.&nbsp; Before that I'd been in engineering software, FORTRAN mostly but some C and even BBC Basic.<br /><br />So Perl is a loosely typed language, identifying variables more by some form of structure than by type.&nbsp; Fine.&nbsp; Having Perl categorised as a loosely typed language really helped me feel Perl was a little more solid.<br /><br />Solid is useful.<br /><br />Then we got onto arrays and hashes, neither of which I had misunderstood, although, back in 2002 it was the first time I had encountered associative arrays - but I'm glad the Perl community has decided to call them hashes.&nbsp; <br /><br />Not least, because hash is a lot easier and quicker to type than associative array.&nbsp; 4 chars as opposed to 16.&nbsp; (Another Perl habit - less typing == good.)<br /><br />So .. hashes and arrays.&nbsp; Originally, I had a hard time remembering where to use the (), [], and {} with hashes and arrays.&nbsp; One part, accessing an array element or a hash key value pair was easy - $arr[$index] for arrays and $hash{key} for hashes.<br /><br />The next part, defining hashes and arrays wasn't so easy and in my early years I used to use () to define an array and {} to define a hash.&nbsp; Then I would have problems accessing hash key value pairs and never quite understood why.<br /><br />Today</p><p>#!/usr/bin/env perl<br /></p><pre>use Modern::Perl 2011;<br />use autodie;<br /><br />my %hash = { fred =&gt; 'wilma',<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; barney =&gt; 'george'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; };&nbsp; <br />say %hash;<br />say $hash-&gt;{barney};<br /></pre>gives these errors<br /><br /><pre>Global symbol "$hash" requires explicit package name at ./test.pl line 11.<br />Execution of ./test.pl aborted due to compilation errors.<br /></pre>Which helps a lot.&nbsp; As a beginner I rarely understood my mistake.<br /><br />Changing<br />&nbsp;<br /><pre>say $hash-&gt;{barney};<br />to <br /></pre><pre>say $hash{barney};<br /></pre>today gives <br /><pre><br />Reference found where even-sized list expected at ./test.pl line 6.<br />Use of uninitialized value $hash{"HASH(0x111d998)"} in say at ./test.pl line 10.<br />HASH(0x111d998)<br />Use of uninitialized value in say at ./test.pl line 11.<br /></pre>which is an improvement (of sorts!) in that the program doesn't completely die - it just declares a warning about $hash{..}.<br /><br />There are two solutions to this error - declaring a hash ref or properly declaring a hash.<br /><br />I tended to use the former<br /><pre><br />my $hash = { fred =&gt; 'wilma',<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; barney =&gt; 'george'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; };&nbsp; <br /></pre>and then of course $hash-&gt;{barney} would work.&nbsp; And the dereferencing of hashes of hashes got to be fun.&nbsp; <br /><br />I finally drummed it into myself, via copious re-readings to the Camel book, that hashes could be defined via %hash = (...); but it took some time to drum that in.&nbsp; <br /><br />As a consequence, I felt I didn't quite hook into the language.<br /><br />The bit that made my day at Perl School is really, really <i>quite simple</i>.<br /><br /><b>Both </b>array and hash definitions are<b> <i>list assignments</i>.</b>&nbsp; <br /><br />So simple, and so clear.&nbsp; And worth it; <em>really</em> worth it.&nbsp; <br /><br />I felt like I had finally landed on Planet Perl.&nbsp; <br />(Probably a bit of an exaggeration but on solid ground at last!)<br /><br /><br /> <p></p>
]]>
        

    </content>
</entry>

</feed>
