December 10th 2007

Javascript Framework Speed Tests

A coworker just pointed out a cool JS speed test to me today. The folks over at MooTools have a handy dandy speed test to compare the top three JS framework libraries to each other. As you can see, Prototype seems to be optimized for Firefox, JQuery for IE and MooTools appears to be a nice middle ground for comparable performance cross browser.

http://mootools.net/slickspeed/

No Comments yet »

October 4th 2007

Page mySQL query results through your favorite pager

Every time I connect to a mysql server, I see the “Type 'help;' or '\h' for help. Type '\c' to clear the buffer.” notice but just ignore it. This morning I decided to ‘\h’ just to see what the in-line help was and found a score of useful commands I had never known about before, possibly the most useful one being ‘\P’.

pager (\P) Set PAGER [to_pager]. Print the query results via PAGER.

if I had a nickel for every minute I spent changing the scrollback of my terminal and then scrolling back for pages and pages to investigate query results I’d be a thousandaire. A simple ‘\P less;‘ now makes my life a little easier. Of course, if you don’t think less is more, you can ‘\P more;‘ instead.

For the curious, here’s the totality of the ‘\h’ output:

List of all MySQL commands:
Note that all text commands must be first on line and end with ‘;’
? (\?) Synonym for `help’.
clear (\c) Clear command.
connect (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter. NOTE: Takes the rest of the line as new delimiter.
edit (\e) Edit command with $EDITOR.
ego (\G) Send command to mysql server, display result vertically.
exit (\q) Exit mysql. Same as quit.
go (\g) Send command to mysql server.
help (\h) Display this help.
nopager (\n) Disable pager, print to stdout.
notee (\t) Don’t write into outfile.
pager (\P) Set PAGER [to_pager]. Print the query results via PAGER.
print (\p) Print current command.
prompt (\R) Change your mysql prompt.
quit (\q) Quit mysql.
rehash (\#) Rebuild completion hash.
source (\.) Execute an SQL script file. Takes a file name as an argument.
status (\s) Get status information from the server.
system (\!) Execute a system shell command.
tee (\T) Set outfile [to_outfile]. Append everything into given outfile.
use (\u) Use another database. Takes database name as argument.
charset (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings (\W) Show warnings after every statement.
nowarning (\w) Don’t show warnings after every statement.

No Comments yet »

September 24th 2007

Terry Chay: Why PHP triumphs over Ruby

Terry Chay writes another hilarious post about PHP vs. Ruby

“I’m really low on my scatological count here…I’m sorry I didn’t crack enough jokes or use enough [cuss words], but I’m sure people will forgive me. They can just attend one of my talks and get their cuss quota for the year. And if not, coding these web apps themselves involves a lot of swearing—a lot of blood, sweat and swear.”
—me on Pro PHP Podcast

I’m not posting my talk yet because I have to give it again at ZendCon. So here is a bit of a teaser.

[Do not hit the jump if you are easily offended. I really mean it!]

No Comments yet »

September 15th 2007

Being nice With Long Processes

Every once and a while, I find that I need to compile code on a machine where many other more important things are running. In my case, I generally find myself recompiling PHP on a machine that also runs my very active forum website [http://forums.reebosak.net] in addition to many other websites, including this one. Running a long build is not exactly the nicest thing to do to users who are trying to go about their normal browsing, where a long build would cause really long page loads since the processor is so busy. The way to deal with this is by being nice (and renice).

from the UNIX man page:

NAME

       nice - run a program with modified scheduling priority

DESCRIPTION

       Run  COMMAND  with an adjusted niceness, which affects process schedul-
       ing.  With no COMMAND, print the current  niceness.   Nicenesses  range
       from -20 (most favorable scheduling) to 19 (least favorable).

To set a processes niceness run it with the nice command:

[brien@toothbrush ~]$ nice -n 5 make // run make with +5 niceness (lower priority)
[brien@toothbrush ~]$ ps aux |grep make
brien      4560  0.0  0.1  5040 1180 pts/1    SN+  18:36  0:00 make
[brien@toothbrush ~]$ sudo renice -3 4560 // change the niceness of the process to -3 (higher priority)

No Comments yet »

September 11th 2007

Auto Converting PHP Objects to Strings using the __toString() ‘Magic Method’

Ever wished there was a way to echo or print a PHP Object as if it were just a string and have it print something more usefull than Object id #1? In PHP5+ there is a Magic Method __toString() that will allow you to specify how you want your Object to react when converted to a string.

<?php
class Person{
  private $firstName="Brien";
  private $lastName="Wankel";
  private $age="30";
  ...
  public function __toString(){
      return "{$this->firstName} {$this->lastName}";
  }
}
$me = new Person();
echo("My name is {$me}"); // prints: My name is Brien Wankel
?>

No Comments yet »

August 26th 2007

Web Development With PLT Scheme

I just ran across this article about using PLT Scheme to write web applications. It’s a great read and a good introduction to Sceme and how it can be used to write “real” programs, not just academic assignments.

From time to time people ask how to develop for the web with PLT Scheme on the PLT mailing list. The quick answer is “Just as in any other language”, but that’s not how to get people hooked on Scheme. To write a decent web-application require knowledge of a range of subjects such as HTML, databases, servlets, and web-servers. For some reason there is a lack of tutorials on these subjects, so I have decided to make an attempt at writing, if not a complete tutorial, then an elaborate get-started example.

[Read the whole article at http://scheme.dk]
[part two: the view]
[part three: an intermezzo]
[part four: the control]

No Comments yet »

July 12th 2007

Learn Python in 10 Minutes

From http://www.poromenos.org/:

So, you want to learn the Python programming language but can’t find a concise and yet full-featured tutorial. This tutorial will attempt to teach you Python in 10 minutes. It’s probably not so much a tutorial as it is a cross between a tutorial and a cheatsheet, so it will just show you some basic concepts to start you off. Obviously, if you want to really learn a language you need to program in it for a while. I will assume that you are already familiar with programming and will, therefore, skip most of the non-language-specific stuff. The important keywords will be highlighted so you can easily spot them. Also, pay attention because, due to the terseness of this tutorial, some things will be introduced directly in code and only briefly commented on.

Go to the article >>

No Comments yet »

May 30th 2007

The One Line Cross-Browser Ajax Request Object

The simple task of getting a working XMLHTTPRequest object can quickly turn into a mess of if/then browser checking. With the help of one of my favorite operators, the ternary operator, this becomes a simple one-liner:

  reqObj = window.XMLHttpRequest
      ? new XMLHttpRequest() :
        new ActiveXObject("Microsoft.XMLHTTP");

There you go, it’s that easy? Now, of course, this is an extremely stripped down browser check, with no error checking, so depending on your need, this may not be the best way to go.

No Comments yet »

May 4th 2007

Easily Syncing Remote Files in Komodo With Unison

In my day to day work, I edit code on one machine, then move the new code to a different machine to build packages for installation and testing. Until recently I was doing one of two things:
1) Mounting an SMB/NFS share and editing the remote files directly
2) Using rsync to copy files back and forth from my dev machine to my build machine

Both methods “worked” but had their downsides. With the remote SMB, I had to have network access if I wanted to work. Since I was also directly editing the source, I only had one copy and if I hadn’t checked in to CVS yet, and something went wrong, I could be screwed. I couldn’t work on anything if I was not connected to the corporate network here at work (ether physically, or via ssh tunnel).
rsync worked alright, but the syncing is one way, so I had to do some weird double syncing to make sure all the changes were propagated. I had to be really careful with edits because if two files were changed in both locations, I would again screwed - no merging. One side of the changes would always be lost. The other downside to this was that if the system times on each machine were not *exactly* the same, rsync would sync files that were not edited, which changed the file’s timestamp, which then caused CVS to see them as edited. It was a mess.

A couple of weeks ago I found Unison:

Unison is a file-synchronization tool for Unix and Windows. It allows two replicas of a collection of files and directories to be stored on different hosts (or different disks on the same host), modified separately, and then brought up to date by propagating the changes in each replica to the other.

With Unison, you simply set up the source and destination directories, which can be local or remote and then tell it to sync. There are optional ways of connecting to remote directories, I personally use ssh, which then uses an ssh tunnel I have set up in my ssh_config file.

This worked great, but I soon grew a little tired of editing files in Komodo, flipping to my Unison window, syncing, then flipping back to Komodo. Then it hit me, “why don’t i just set this sync up as a toolbox item?”. Unison has a command line interface, so after a quick read of the ‘-help’ output, I created a toolbox item with unison -batch -auto; (read the ‘-help’ to customize your sync with your own preferences), then set up a keyboard shortcut CTRL+ALT+S to trigger it. Now I can happily edit my code on my laptop and sync with a quick flick of the fingers.

NOTE: I have this set up in Linux, but since both Komodo and Unison are available for Windows, it should be trivial to accomplish the same in that environment

No Comments yet »

January 26th 2007

An easier way to do simple matches with PHP

Tired of trying to figure out that perfect Perl-style regular expression when all you need to do is a simple match? Well, toss away that ereg or preg_match call you’ve been working on for the last 20 minutes and use your new friend fnmatch(). fnmatch will let you match using POSIX shell command style patterns, which may be much much easier for a non regex guru to wrap their head around.

<?php
if (fnmatch("*gr[ae]y", $color)) {
  echo "some form of gray ...";
}
?>

This function is only available on POSIX compliant systems, so all you Windows PHP users are out of luck.

Enjoy!

No Comments yet »

Next »