Tuesday, February 24, 2009

Javascript and NaN

I always seem to forget but you have to use the function isNaN to test javascript variable. I always want to write it wrong with value == NaN...

Here is an example of it (not a great one but a general idea of what's going on):
 var num = Number(v);

if ( isNaN(num)) console.debug("v is not a number");
Such a strange thing that num isn't null or that it doesn't throw an exception. The crazy typing stuff inside of languages :)

Friday, February 20, 2009

Template Toolkit virtual methods with less mess

Template Toolkit has a very cool feature called Virtual Methods. It provide methods on native Perl5 data types.

An example is this (In TT syntax):
[% listy = [ 47, -47, 42, -55, 4, -24 ] %]
[% listy.size %]
[% listy.join(', ') %]
Often its called autoboxing. Well TT allows you to define your own. The details are available in the TT VMethod Manual. In that page there is a TODO about a method called define_vmethod.

Since I like to use TT from CGI::Application and I don't like to actually see its guts. I choose to find a way to add a vmethod without the unpretty $Template::Stash::LIST_OPS->{} syntax. I think that is quite unfriendly to TT noobs like most of my coworkers.

I found that define_vmethod is available off the TT Context object. This is available with $tt->context or in my case off plugin TT object in CGI App $self->tt_obj->context.

The parameters are context->define_vmethod( $type, $name, \&implementation )
  • $type - data type, 'scalar', 'list', 'array' or 'hash'
  • $name - the name for you method 'abs', 'smash', 'love'
  • \&implementation - sub reference that does the method
Here is my case, where I really wanted to do:
[% IF indexes.$rate.value.abs > 10 %]

Here is the implementation:
## inside my CGI::App run mode
sub qa {
my $self = shift; ## grab cgi::app object

$self->tt_obj->context->define_vmethod( 'scalar', 'abs',
sub { return abs( shift ) } );
....
}
The parameters passed to the sub reference depend upon the datatype. But after the data type value then additional parameters are passed, so I could have added parameters to the abs call. E.g.
[% indexes.$name.value.abs(10) %]
In that case the parameters would have been: my @_ = ( $list_value, 10 );

First parameter for each type:
  • scalar - value
  • list||array - reference to list
  • hash - reference to hash
In another post, I'll write an example of either or both of those.

Wednesday, February 11, 2009

vim - insert current filename

When writing comments in source code or for some other reason than I can think of right now, I like to include the current filename into current vim session.

How?
  • Enter insert mode
  • use following key sequence: ctrl-r %
POOF! You have the filename (it does include the path).

In vim % is the current filename. It is very useful when writing mappings or running commands from the : prompt.

Wednesday, February 4, 2009

Frozen Perl 2009

Frozen Perl 2009 is right around the corner, well its on Sat Feb 6. Be there or be cubed (ice cube)...

Not really goodbye to Google Notebook

I really like Google Notebook but it seems that Google doesn't see it as having many more features to add. Fair enough, they aren't killing it, just not adding to it. Maybe that is called feature complete.

Today at work I was wishing for an Open Source version of it that I could install locally for priority work stuff. Maybe its time to start creating one... with all that free time :)

Google Notebook Blog - Statement about Development Stopage

Monday, February 2, 2009

Weird make/gmake error

When gmake'ing tools, I started to get strange errors like:
/bin/bash: complete: command not found
/bin/bash: /home/users/lcarmich/etc/bash_completion: line 220: syntax error near unexpected token `(('
What I forget is that I build my own newer bash version (3.x) to be able to use the shell completions (and many other features) and the system default is 2.x. Don't ask about that. But gmake grabs the shell first in your path (older bash in my case).

I found that you can grab different version parts from bash in the BASH_VERSINFO array. Well I only really care about major version and this is available in variable BASH_VERSINFO or array element 0 BASH_VERSINFO[0]

I updated my bashrc to check version then load completions:
if [ -r $BASH_COMPLETION -a $BASH_VERSINFO -ge 3 ]; then
. $BASH_COMPLETION
fi
For future reference you can get array elements in bash with ${VARNAME[INDEX]}. For example:
mnsdev11 (netcat-0.7.1)$ echo $BASH_VERSION
3.2.0(1)-release
mnsdev11 (netcat-0.7.1)$ echo $BASH_VERSINFO
3
mnsdev11 (netcat-0.7.1)$ echo ${BASH_VERSINFO[0]}
3
mnsdev11 (netcat-0.7.1)$ echo ${BASH_VERSINFO[1]}
2
mnsdev11 (netcat-0.7.1)$ echo ${BASH_VERSINFO[2]}
0
mnsdev11 (netcat-0.7.1)$ echo ${BASH_VERSINFO[3]}
1
mnsdev11 (netcat-0.7.1)$ echo ${BASH_VERSINFO[4]}
release
Done.