Tag Archives: Perl

Magic DBI incantation returns array of hashes keyed by column names

my $sql = "select foo, bar, baz from some_table;"; my $result = $dbh->selectall_arrayref($sql, {Slice => {}}); #$result = [ # {foo=>'some data', bar=> 'more data', baz=>'more data'}, # {foo=>'data for row 2', bar=>'....', baz=>'...'}, # {foo=>'data for row 3', bar=>...etc #] It’s the “Slice => {}” part that does it.
Posted in Software Development | Also tagged , | Leave a comment

Web Applications on a Mac: hello world perl CGI

Step 1) enable web sharing a.k.a. Apache web server: go to system preferences => sharing, and click Web Sharing Step 2) make sure Apache is running by going to the address ‘http://localhost’ in your browser. You should see the message “It Works.” Step 3) In the default setup,  http://localhost/cgi-bin is aliased to the directory /Library/WebServer/CGI-Executable, [...]
Posted in Software Development | Also tagged , | Leave a comment

Perl: Inheritance Seems Broken (Because It Requires an Instance)

I want to demonstrate how in Perl, when one class inherits from another, methods from the parent class are inherited as long as the derived class is being called in an object oriented manner. So here is some code for the parent class: package Animal; use warnings; use strict; sub new { my ($class) =@_; [...]
Posted in Software Development | Also tagged , | Leave a comment

toString() For Perl Objects

I’m working my way through “Programming Perl” and pulling out the little tidbits that I find most useful.  The following is a method for overloading the double quote operator (Perl’s version of Java toString()) so that you can just print an object with a statement like ‘print $object1′ and get something coherent. The basic approach [...]
Posted in Software Development | Also tagged | Leave a comment

Private Methods in Perl Objects

It is common to use a naming convention to indicate that a perl method is private.   I have seen the double underscore used, as in __initialize or __disconnect. Reading ‘Programming Perl’ however, there are a number of methods for actually making code private.  Below is an example of one method, using a subroutine reference.  Someone [...]
Posted in Software Development | Also tagged | Leave a comment