Version 11 of main script

Updated 2014-06-05 21:54:53 by AMG

The main script is the primary script that is executed by the interpreter, e.g. tclsh or wish.

Description

One common technique is to have a script run a self test if it detects that it is the main script. Naive approaches only compare info script with $argv0. A somewhat more robust approach that usually works with pkg_mkIndex looks only at the file tail of those two values. The most robust approach fully normalizes those two values, including the last component, which a simple file normalize does not resolve.

The most complete approach:

if {[info exists argv0] && [
    file dirname [file normalize [info script]/...]] eq [
    file dirname [file normalize $argv0/...]]} {

    #do stuff
}

AMG: I'm not sure I fully understand the purpose of the /... in this code. From the above description, I'm assuming it's a dummy component so that [file normalize] resolves all components of the true path.

A common and fairly robust approach:

if {[info exists argv0] && [file tail [info script]] eq [file tail $argv0]} {
    #do stuff
}

The negative approach, also quite common:

if {![info exists argv0] || [file tail [info script]] ne [file tail $argv0]} {
    return
}

# test/standalone code follows

The more simple and naive approach to detection method directly compares info script with $argv0:

if {[info exists argv0] && $argv0 eq [info script]} {
    #do stuff
}

or, as formulated in a comp.lang.tcl posting by DKF:

if {[string equal $::argv0 [info script]] || [array exists ::embed_args]} {
    main
} 

AMG: Which posting would that be? What is ::embed_args? It must be something specific to the script DKF was describing.

authors

dkf
CJL
AMG: I'm not sure who this is... is a full name available?
PYK
RS