Archive for December, 2008

PHP vs Perl 0

Being my first real post, I thought I would show something that I really benefitted me by moving to PHP from Perl, but that ultimately helped me whenever I maintained Perl code.

In pulling data from a MySQL database for example, one might use the following PHP code:

1
2
3
4
5
6
7
// MySQL database connection code previously defined
$statement = mysql_query("select first_name, last_name from users");
 
while ($user_data = mysql_fetch_assoc($statement))
{
	echo "First Name: " . $user_data['first_name'] . " Last Name: " . $user_data['last_name'] . "\n";   
}

Line 2: this is our MySQL query.
Line 4: our while loop that goes through each row of returned MySQL date. mysql_fetch_assoc puts the MySQL data into an associative array. (see array detail below).
Line 6: here we echo everything out to the browser.

This would obviously pull all the first and last names from a users table, and print them out nice and neatly. Notice that I used $user_data['first_name'] and user_data['first_name'] to pull the first name and last name, respectively. User data is built like so:

Array
(
    [first_name] => John
    [last_name] => Doe
)

So when I code $user_data['first_name'], what I’ll get is “John”.

The same code in Perl might go something like this:

1
2
3
4
5
6
7
8
9
# $dbh, or the MySQL database connection, was defined early in the code
my $statement = "select * from users";
my $sth = $dbh->prepare($statement) or die "can't prepare the statement";
my $rv = $sth->execute or die "can't execute the query: $sth->errstr";
 
while (my $user_data = $sth->fetchrow_hashref())
{
	print "First Name: " . $user_data->{'first_name'} . " Last Name: " . $user_data->{'last_name'} . "\n";   
}

Line 2: our MySQL query.
Line 3: we prepare the statement for execution.
Line 4: we execute the query, calling die if there is an error.
Line 6: similar to the PHP while, we put the MySQL data into a Perl hash, very much like a PHP associative array.
Line 8: here we print it all out.

The output of this code is exactly the same as the PHP code. Sadly, it took me learning PHP to realize that you could even do this with MySQL data, and so … for many years I annoyingly coded my MySQL data in perl using indexes ($user_data[0] instead of $user_data->{‘first_name’}).

Anyway, I hope this helps understand how to make Perl do what PHP does so easily. Enjoy!

Storples reborn 1

Today I decided that Storples had suffered enough.  From it’s beginnings as a place for me to learn HTML, to a place where friends would gather to gawk and my private content.  It did, however, always serve the purpose of providing me a place to learn to code … when I went from HTML to Perl, and briefly, Perl to PHP.

In more recent versions, I attempted to bring it up to speed with face lift after face lift, all the while not improving on the one thing it lacked … content.

So here we are today.  I am sadly abandoning the old ways … detailing “what I’ve been up to” or “check out this sweet car”.  No, today I turn Storples into what I have wanted it to be for quite a long time … a technology blog.

I am sure to alienate most, if not all my long time readers … and I’ve come to terms with that.  Hopefully these half-dozen or so people will find it still interesting, but if not, I appreciate the attention you gave the site all these years.  I couldn’t have done, or rather, wouldn’t have done it with out you.  Sure, there will probably never be a GOTW again, but surely you’ve found better places to find that stuff by now!

Anyway, here we go …