DISCLAIMER: THESE PAGES ARE STILL UNDER CONSTRUCTION. NO CODE EXAMPLE BEEN TESTED YET.

Perl Traps

Losing track of references - keep track of what needs to be dereffed


[Previous Page] |[Next Page] Table of Contents: small | med | large

If you use references to scalars, it is often easy to forget to dereference them. When you use references to arrays or hashes, it is usually pretty easy to keep track of them, though having good naming conventions for your variables helps. My experience has been I generally don't forget to deref my arrays.

Scalars are a different story though. If I did this:

	$currToken = 31.5;
	$node{'tx'} = \$currToken;
then it would be really easy for me to forget that it was a scalar ref (pointer to the scalar $currToken) rather than a simple scalar. So if you said:
	$tx = $node{'tx'};
then $tx would wind up looking like SCALAR(0x810201c), hardly the "31.5" we were expecting. Of course, we would have to access this with:
	$tx = ${$node{'tx'}};

So at this point, it would seem that saying

	$currToken = 31.5;
	$node{'tx'} = \$currToken;
is a pretty idiotic idea. Well, that's not exactly true. There are some circumstances where you may want these.

In fact, nodes are a good example. If you are using anything special like Maya or Inventor nodes, you might want to designate a field type. With the bless command, you can actually give a type to a scalar. The trouble is that you can only bless a reference. Generally, a reference to a structure of some sort, but it can be a simple scalar. bless is a nice way to just add a litttle extra data to your variable. For instance, you could say:

	$currToken = 31.5;
	$node{'tx'} = bless(\$currToken, 'FloatField');
This will let you both assign the value 31.5 to your tx field and assign it the type 'FloatField'. I find this pretty useful with scene graphs, but I don't take advantage of it with scalars that much otherwise.
© 2001 Steve Hwan, hostname: @pacbell.net, username: svhwan
You should probably use the word "PERL" in the subject line to get my attention.
Last Modified: Wed Apr 25 01:53:55 2001