Monday, October 12, 2009

Callback Functions - Scoping

According to Wikipedia, a callback function is executable code that is passed as an argument to other code. It allows a lower-level software layer to call a subroutine (or function) defined in a higher-level layer.

Callback functions are used very frequently in event driven software systems. Whenever an event happens, a function is called.

The idea is simple, but many people tend to become confused when using these things. The problem that they encounter is that people tend to run into problems with the scope of variables that they are using.

Part of the problem is remembering that the callback function may form a closure. When the callback function is executed, the scope consists of the callback function's code and references to any variables of the function containing the callback function reference. The converse is not true; the function containing the callback function reference cannot use the arguments and variables of the callback function.

Another thing that can confuse people using callback functions is the use of global variables. Since callbacks are usually called in response to events, it may not be clear what the current state of the system is and using any predefined global variable may result in inconsistent results.

In addition to global variables, variables using the same name in the code containing the reference to the callback function and with in the callback can sometimes confusing to people. In this case, the variable within the callback takes precedence.

# example function with closure

function makePowerTo(x) {
return function(y) {
var result = y;
for (var i = 1; i < x;i++)
result *= y;
return result;
};
}
var square = makePowerTo(2);
var cube = makePowerTo(3);
print(square(2)); // 4
print(cube(2)); // 8

square
and cube are both closures. They share the same function body definition, but store different environments. In square's environment, x is 2. As far as cube is concerned, x is 3.

Monday, October 05, 2009

Finding and Deleting Files

Today, I was trying to remove a bunch of temporary files using the DOS command prompt on Windows Vista. What a pain in the butt!

Let's assume that I have a bunch of unwanted ".tmp" files in the sub-directory "subdir". The directory "subdir" also has other sub-directories containing more files that I would like to delete.

For those of you familiar with Linux, to find and delete files its as easy as the following command:
find ./subdir/ -name '*.tmp'-exec rm -f {} \;
Unfortunately, I couldn't muster something like that up on Windows Vista. I tried stuff like
dir /B /S | find ".tmp"
This produced the full path of the files that I no longer wanted. When I then tried to pipe those files to del, like
dir /B /S | find ".tmp" | del
Vista just said 'The syntax of the command is incorrect.'

To make a long story short, I was finally able to come up with the following:
for /R %i in ("*.tmp") do del %i
Do you have an alternative way of finding and deleting files?

Thursday, October 01, 2009

Trim whitespace from string in PERL

I like Java's String.trim() method, and so I thought I would add one for PERL:

sub trim {
my ($self, $text) = @_;
$text = $self
if ref(\$self) =~ m/^SCALAR/i;
# return empty string if $text is not defined
return "" unless $text;
$text =~ s/^\s+//;
$text =~ s/\s+$//;
return $text;
}

That is all that there is to it!

You can add this to your module, and do $self->trim($string) or you can do trim($string) ... If you have anything to add, let me know!