ArmanHunanyan
ArmanHunanyan

Reputation: 1015

Getting line number in tcl 8.4

I need to get invocation line number of tcl proc inside it’s body.

Starting from 8.5 tcl have info frame command which allows following:

proc printLine {} {
    set lineNum [dict get [info frame 1]  line]
}

I need the same for 8.4

Upvotes: 2

Views: 1502

Answers (2)

user6804435
user6804435

Reputation: 39

I'm using tcl 8.5, but it should work on version 8.4. here is:

#!/usr/bin/tclsh

puts "tcl version: $tcl_version"

proc linum {} {
    if {![string equal -nocase precompiled [lindex [info frame -1] 1]]} {
        return [lindex [info frame -1] 3]
    } else {
        return Unknown
    }
}

puts "call proc @line:[linum]"

and the result is:

tcl version: 8.5
call proc @line:13

you can reference info frame for more details

Upvotes: -1

Donal Fellows
Donal Fellows

Reputation: 137567

It's not available in 8.4; the data wasn't collected at all. I guess you could search for a unique token in the line, but that'd be about all.

proc lineNumber {uniqueToken} {
    set name [lindex [info level 1] 0]
    set body [uplevel 2 [list info body $name]]
    set num 0
    foreach line [split $body \n] {
        incr num
        if {[string first $uniqueToken $line] >= 0} {
            return $num
        }
    }
    error "could not find token '$uniqueToken'"
}

Note that 8.4 is not supported any more. Upgrade.

Upvotes: 5

Related Questions