Electricmonk

Ferry Boender

Programmer, DevOpper, Open Source enthusiast.

Blog

RssMerger v0.5

Thursday, February 16th, 2006

Version 0.5 of RSSMerger is available. Changes in this release include:

  • (My)SQL insert queries can now be generated for inserting into a database.

Unicode for programmers

Thursday, February 16th, 2006

Programmers should understand Unicode because
* It is one of the cornerstones of software internationalization.
* The Web is full of Unicode data.
* XML and HTML are based on Unicode.
* WinNT-based operating systems use Unicode for all string values internally.

This article is a short course on Unicode programming.

Every programmer, but especially Web developers, should read it.

PHP: Directly Assign Exploded Parts To Variables

Saturday, February 11th, 2006

Many languages (amongst them Python and Perl) provide a way to directly assign the parts of a split string to variables. The syntax usually used is something like:

firstname, lastname = "John,Doe".split(",");

or something similar. PHP does not support such a syntax. The programmer instead has to perform all spliting, checking and assigning himself.

This little snippet of code allows you to directly assign parts of an exploded string to variables or -if the exploding does not match the number of variables- assign defaults.

function explodeAssign($string, $seperator, $vars) {
    $temp = explode($seperator, $string);
    if (count($temp) != count($vars)) {
        return(false);
    } else {
        for ($i = 0; $i != count($temp); $i++) {
            $vars[$i] = $temp[$i];
        }
        return(true);
    }
}

$string is the string you want to explode.
$seperator is the seperator you want to use.
$vars is an array containing pass-by-reference variables. (this is important, otherwise it won’t work)

Some examples:

if (!explodeAssign(
  "ferry,boender,25",
  ',',
  array(&$firstname, &$lastname, &$age))) {
    $firstname = "John";
    $lastname = "Doe";
    $age = "20";
}

print("Hello $firstname $lastname. You are $age years old.n");

if (!explodeAssign(
  "ferry,boender",
  ',',
  array(&$firstname, &$lastname, &$age))) {
    $firstname = "John";
    $lastname = "Doe";
    $age = "20";
}

print("Hello $firstname $lastname. You are $age years old.n");

The output of which will be:

Hello ferry boender. You are 25 years old.
Hello John Doe. You are 20 years old.

Hope it’s useful.

Update: Not as useful as I thought, as PHP has a list() language construct:

list($firstname, $lastname, $age) = explode(",", "ferry,boender,25"));
print($firstname); // ferry

Ctags and Object Oriented PHP5

Saturday, February 11th, 2006

About CTags

Ctags is a great utility. It creates a file with a list of ‘tags’ that are found in various program sources that it finds in the directory where you run it. These tags can then be read by editors so that they know where in the source to find, for instance, the definitions of functions, methods, constants and variables. Or as the CTags homepage describes it:

Ctags generates an index (or tag) file of language objects found in source files that allows these items to be quickly and easily located by a text editor or other utility. A tag signifies a language object for which an index entry is available (or, alternatively, the index entry created for that object).
Editors like Vim use it to let you peek at or jump to the definition of a function; a feature commonly thought to only exist in bloated IDE’s such as Eclipse.

PHP5 Object Oriented

Unfortunately, CTags doesn’t support PHP’s Object Oriented features very well. All you’ll get is globally defined constants and functions. It also generates tags for classes, but that’s it. No support for methods or anything! Hardly very useful in it’s current form, I’d say. Thankfully there is a patch available that greatly extends CTags’ Object Oriented PHP support.

Fixing CTags

To fix CTags so that it does support PHP in a not-completely-retarded fashion, you’ll have to patch and compile CTags yourself. Here’s how:

Download CTags v5.5.4.
Download the patch (alternatively and in case the patch disappears from the previous location, download it from electricmonk’s mirror.

Next, patch, build and install CTags:

[todsah@jib]~/download$ tar -vxzf ctags-5.5.4.tar.gz
[todsah@jib]~/download$ patch ctags-5.5.4/php.c ctags-php5.patch
[todsah@jib]~/download$ cd ctags-5.5.4
[todsah@jib]~/download/ctags-5.5.4$ ./configure
[todsah@jib]~/download/ctags-5.5.4$ make
[todsah@jib]~/download/ctags-5.5.4$ sudo make install

The new ctags binary will be installed in /usr/local/bin by default.

Now you can use the self-built version of ctags to create tag files that contain enough information about your PHP’s source code to actually do something meaningful:

[todsah@jib]~/plugwiki$ /usr/local/bin/ctags -R --extra=+q

Fire up your Vim editor (you are using Vim, aren’t you?? Get off my site, you dirty Emacs user!) and start exploiting your newly created tags file.

Using ctags in Vim

In case you don’t know how to use ctags from within Vim, don’t worry. It’s really easy. Move the cursor on top of some method call and press Ctrl-]. Vim will jump to the definition of the method/function/constant/whatever that the cursor was currently on. To jump back to where you came, press Ctrl-t. It’s a simple as that.

An example. Suppose I have the following code:

if (!$page->hasAccess(

I’ll position my cursor on hasAccess and press Ctrl-]. Vim will now jump to this text:

  public function hasAccess($user, $access)
  {
    $userId = $user->getUserId();

When I type Ctrl-t, Vim will jump back to:

if (!$page->hasAccess(

and I can complete my code.

Another cool tool for Vim that uses tags is TagList.

The “Tag List” plugin is a source code browser plugin for Vim and provides an overview of the structure of source code files and allows you to efficiently browse through source code files for different programming languages.
You can see TagList in action in this screenshot of Vim.

(Thanks to Michiel van Baak for corrections in this post.)

www.SorryNorwayDenmark.com

Monday, February 6th, 2006

www.SorryNorwayDenmark.com. Finally some people with sense.

Floating point stuff in Bash

Monday, January 30th, 2006

Shell scripting is powerful, but unfortunatelly it gets less easy if you want to perform floating point calculations in it. There’s expr, but it only handles integers:

[todsah@jib]~$ echo `expr 0.1 + 0.1`
expr: non-numeric argument

If you wish to perform floating point calculations in shell scripts, you can use the bc tool: “bc – An arbitrary precision calculator language“.

Some examples:

[todsah@jib]~$ A=`echo "0.1 + 0.1" | bc`; echo $A;
.2

However, dividing two numbers in bc, which would result in a fractured (floating point) number, doesn’t work out-of-the-box. So you’ll need to set the scale variable:

[todsah@jib]~$ A=`echo "10 / 3" | bc`; echo $A;
3
[todsah@jib]~$ A=`echo "scale = 2; 10 / 3" | bc`; echo $A;
3.33

You can also evaluate boolean expressions using bc:

[todsah@jib]~$ if [ `echo "10.1 < 10" | bc` -eq 1 ]; then echo "10.1 < 10"; fi
[todsah@jib]~$ if [ `echo "10.1 > 10" | bc` -eq 1 ]; then echo "10.1 > 10"; fi
10.1 > 10

This function might prove to helpful:

function fpexpr() {
    scale=$1
    shift
    expr=$*

    echo `echo "scale = $scale; $expr" | bc`
}

For example:

[todsah@jib]~/notes/work/organisatorisch$ A=`fpexpr 5 "10 / 3"^J`; echo $A;
3.33333

bc can do a lot more than this. Consult the manual page for other handy stuff.

Learn your language’s basic features

Saturday, January 28th, 2006

Sometimes I’d really like to bash in my own head with a 2×4. I just spent at least 20 minutes figuring out why the boolean expression

($options & PW_SORT_DATE == PW_SORT_DATE)

didn’t evaluate to true, even though $options had a value of 2 and so did PW_SORT_DATE. Then I suddenly realised that PHP’s == operator precedes the & operator.

(($options & PW_SORT_DATE) == PW_SORT_DATE)

works like a charm.

I should know better than not too learn the basics of the programming languages I work with. :)

Monsieur Cannibale

Wednesday, January 25th, 2006

Check out singer/songwriter Monsieur Cannibale. Excellent stuff.

GPLv3 Draft

Tuesday, January 17th, 2006

It’s out in the wild: The GNU GPL v3 Draft.

Change blindness

Friday, January 13th, 2006

This is pretty cool. Try to see if you can spot the change that occurs during the flicker (after about 1.5 seconds) in this image:

Kayak

Now try it in the same image without the flicker:

Kayak

More information in this paper

The text of all posts on this blog, unless specificly mentioned otherwise, are licensed under this license.