Sunday, November 23, 2008

Quotes

Quoted
Quotes are widely used in scripting languages, including (but not limited to) Bash, SQL, Python, Tcl, Perl, PHP, JavaScript. However, there is no universal rule for quotes. Most languages support single quote (') or double quote ("), while others have their own quotes like triple quote (''' or """), or braces ({, }). When one should code in a combination of different languages, the quotes would definitely confues programmers, not mentioning variable substitution, regular expression, etc.

Thursday, November 13, 2008

Different outputs in Tcl - Stdout vs Return Value

When both set a {hi} and puts "hi" echo a "hi" in an interactive tcl shell, what is the difference between them?

Clarification on "output"
1. standard output
puts command will output to stdout by default.
% puts "hello"
hello
2. return values
set hi {hello} has a return value hello and will echo back to user
% set hi {hello}
hello

To make things clear, look at this example.
% set ret [puts "hello"]
hello
% puts $ret
(blank)

So that the return value from puts is blank.

Compound statements
Let move on to compound statements.
% set a "hello"; set b "hello2"
hello2
This gives a single line of output from `set b`, and a and b are both set.
% puts $a
hello
% puts $b
hello2

Here's two lines of output from stdout.
% puts "hello"; puts "hello2"
hello
hello2

Importance
Why is this important? What if we would like to store a calculated value into variable a.
% set a [expr 3+3]
6
% puts $a
6
This is what we expected. However, if we wrote another version of expr that output the result into stdout.
% set a [set b [expr 3+3]; puts $b]
6
% puts $a
(blank)
We get nothing.

Generally speaking, it is not a good idea to print to screen from many functions. Because this would make the testing difficult, if not impossible. Usually, assert will compare the return result to expected result, while the output to standard output are ignored. Despite all that, we can still try to test function which output to stdout. The idea is to trap the stdout. This assert for stdout would look like this pseudo code:
procedure assertStdout (expected, command) {
  create out for this instance
  execute command > out
  flush out
  return expected == out
}
One trick is that if multiple assertStdout procedures are running, 'out's must be distinct in order to keep thread-safe.

The echo back of tcl might be of use, but it could confuse people with the output from within procedures. However, it is mandatory to distinguish them for a programmer. Other scripting languages like shell does not have such echo back, but that doesn't mean it is OK to output from many functions for them. Especially, for compicated software that requires a thorough test, standard out or output to file everywhere would definitely bring down the testing.