Version 9 of main script

Updated 2014-06-03 10:42:32 by RLE

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.A

The most complete approach:

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

    #do stuff
}

A fairly robust approach:

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

Another variation:

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 only 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
} 

authors

dkf
cjl
PYK
RS