Monday, December 29, 2008

The power of eval

In my programming environment we use PHP and javascript as our primary programming languages. One very handy construct that both of these languages have is the eval statement, which essentially allows a text string to be converted into and executed as code.

An example in PHP:

if(strpos($cToolTip,'$')) {eval("\$cToolTip = \"$cToolTip\";");}

An example in javascript:

eval("day.onclick= function() {goToDate('" + xday + "');};");

In the PHP example, we are checking if the string $cToolTips has any embedded variables (which always begin with a '
So, if we have:

$fubar = 'abc123'
$cToolTip = "Once upon a $fubar night"

After the eval statement, $cToolTip = "Once upon a abc123 night"

The javascript example, while functionally similar, has another very important advantage when programming in js. Lets say you have a loop, and within that loop you are creating a new elements on a page, and are assigning onclick events to these elements. So far, so good.

However, in our example,

eval("day.onclick= function() {goToDate('" + xday + "');};");

Note that we are assigning xday as a paramater in the goToDate function. This is great, except for one thing. If we did not use eval, every time we incremented or changed xday, it would change in every function in which it had been used.

Thus, the eval statement lets us, in effect, turn a variable into a constant so that we do not have to worry about the value of that variable once the eval statement has been run. That can be very handy.

Saturday, August 9, 2008

I have always been of the opinion that words have meaning, but sometimes I am not sure if this view is shared. In particular, I am troubled by the meaning of the word 'science' in 'science fiction'.

I have always thought of Science Fiction as fiction that is based on the extrapolation of the impact of scientific and technological advances on the human (or alien) condition. Further, the science and technology should have some real basis in science, i.e., what is postulated should be theoretically possible.

Today I received a multi-paged ad from the Science Fiction Book Club. However, like the Science Fiction section of most book stores, it was only about (at best) 30% SF - the majority of the books were horror, fantasy, and semi-porn 'art' collections. Science Fiction is a vastly different genre than the horror-fantasy genre; the former is based on the reasoned application of science, the later is based on whatever the author wishes to pull out of their ass. They are very, very different.

Which takes me back to my point that words have meaning. I would kindly ask the book sellers of the world to take the horror-fantasy titles out of the SF section; I am tired of looking in vain through the SF section for SF books. It degrades the SF genre to associate those other titles with real SF. It confuses the general public as to what SF really is ("Just dragons and naked girls - sorry son, you can't read that!").

In this era where Science and Fantasy are so easily and casually confused in the public arena this would be a small step in making sure that we can distinguish between what we wish were possible and what really is possible.

Regards,

David

Sunday, August 3, 2008

Unsung Hero


Over the past several weeks I have introduced several people to Dia, and all of them were astounded and wondered why they had never heard of this program before - so here it is.

Dia is a diagramming program for Linux (has a windos port as well) that performs all of the basic functions of Visio(tm); Depending on one's skill, very complicated diagrams of any process can be created in this program and saved and/or exported to a variety of formats, including .png, .gif, and and a visio compatable format.

Dia has an extensive library of templates and the ability to import images. If you are a python wizard, you can create new extentions. Etc.

My main reason for using Dia is to communicate with non-technical co-workers. Many times a diagram can communicate technical problems in a manner that the non-technical can understand. It provides a common reference that does not have to rely on technical jargon.

There are other diagram tools for Linux, some of which have nice features, but I find that I always return to Dia because it is just easier to use. This may be due to the fact that I know it best, but I think not.

If you have another (Linux) diagram tool that you like better, let me know why!

Sunday, June 8, 2008

Its the little things

I have been a fedora/RedHat user for a long time, and still am, where servers are concerned. However, I've started using Ubuntu on the desktop, and I'm impressed so far.

I started using Ubuntu because it was the only Distro that would work on my new laptop, a Dell Inspiron 1525. I tried Suse, Debian, Fedora, and several distros that came free in some Linux mags; they would install but not boot, or boot but not recognize major elements (USB, network, hard drives, etc.). I went on line and found that Dell was selling this machine in Europe with Ubuntu, whereupon I downloaded the latest Ubuntu and the rest is history. It works very well.

With one small glitch.

When I use vim, there was no context highlighting. I googled around and found this vim command:

:syntax on

So my question is: Why, why would this function not be turned on by default as it has been in every other distro I've used. What is the reasoning?

There are other weird little things. For instance, Dia on Fedora: When one is exporting a graph as a .png or .jpg, on Fedora Dia will ask you to define the size the graphic file (pixel width x height). But not on Ubuntu, with the exact same Dia version. Why? There is a way around this, of course, which is to run the export from the command line.

These are very, very minor issues. But why do different distros have different defaults for these very common applications? This, at least, should be standardized across all platforms.

Regards,

David

Thursday, May 29, 2008

Cheese

Several years ago one of the things that was really missing from desktop Linux was the use of USB cameras, which were quickly becoming ubiquitous in both the Mac and Windows world. I spent a fair amount of time downloading, compiling, and tweaking, but it became very clear that USB cameras were not (yet) for Linux.

Fast forward to the present and Ubuntu 8 and Fedora 8. USB cameras have become plug and play thanks to the hard work of OSS developers (thanks!). And now that USB cameras have been incorporated into the Linux experience, we have Cheese, a USB camera utility to take pictures and videos from your USB camera (a la Mac). Developed by Daniel G. Diegel and Jaap A. Haitsma , this program is a great tool for engaging your kids (and having some fun yourself).

While it doesn't seem to have made it into the standard builds for Ubuntu or Fedora, it can be installed on each (apt-get install cheese, or yum install cheese). Its a great little tool and a must have for anyone using desktop Linux at home.

Regards,

David

Sunday, May 18, 2008

First Post

I've been reading other people's blogs for some time now and have decided that I have a few things of my own to say. My concerns run to the future direction of our society. Will it be open, expansive, and growing, or closed, defensive, and moribund? My preference is for the former, but I see many trends that are pushing us towards the later, and I would like to point out both trends as I notice them, and see what others have to say about that.

Like everyone else, I am biased by my beliefs, so I will spell out the basics of my philosophy so that there is no mystery as to my motivations. I am:
  • Socially liberal
  • Fiscally conservative
  • A believer in the scientific method
  • Devoted to my family
  • Proudly American (warts and all)
  • Happy to be part of Western Civilization
  • Optimistic about the future
  • A fan of Open Source and Capitalism
  • A believer if facts, not beliefs.
I am, of course, more nuanced than the above list, but the list still provides a guide to where I am coming from.

David