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

Perl Traps

Not every command has array context.


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

I've mentioned earlier that I like to explicitly call my user-defined functions like this:

	&userDefinedFunc($arg1, $arg2, $arg3);
That is, explictly saying & and () to give a clear signal that I've got a function here. There is a slight hiccup to this. Some commands look like they may want a list, but the first argument really isn't a part of the list.

For instance, if I want to print a line to a filehandle, I may say:

	# Right
	print FILEHANDLE $line;
and I may be tempted to say:
	# Wrong
	print(FILEHANDLE, $line);
but that would be wrong because print will not realize that it was supposed to send the $line to a file handle. The file handle needs to be designated before the list of lines to print, but if it just sees a list, it will attempt to just print the list to STDOUT. As warped as this sounds, it will see FILEHANDLE as an argument to print to STDOUT, instead fo printing everything to the file handle FILEHANDLE.

So then it would be tempting to say:

	# Wrong
	print(FILEHANDLE $line);
(neglecting the comma). This too would be incorrect. When Perl sees the parenthesis, it expects to see a list. When it sees (FILEHANDLE $line); then it dies because the compiler doesn't recognize this as a legitimate list.

No, I'm afraid the only way to do this is really

	# Right
	print FILEHANDLE $line;

Some other functions you should look out for are: map, sort, and grep. If you make your own code blocks as the first argument, then the first argument may not be counted as part of a list, so it should not be inside parenthesis.


© 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 22:39:55 2001