1

I have a script that uses the struct::matrix package. The following is basically the code I'm using:

body className::methodName {args} {
    # ...
    ::struct::matrix mat
    set CSV_file_handle [open $CSV_file_path]
    csv::read2matrix $CSV_file_handle mat , auto
    close $CSV_file_handle
    set workbook_list [mat serialize]
    # ...
}

And when executing the file, an error rises (omitted irrelevant info):

$ tclsh script.tcl

invalid command name "mat"
    while executing
"$m columns"
    (procedure "Split2matrix" line 25)
    invoked from within
"Split2matrix $alternate $m $data $sepChar $expand"
    (procedure "csv::read2matrix" line 87)
    invoked from within
"csv::read2matrix $CSV_file_handle mat , auto"
    (object "::classObject" method "::className::methodName" body line ...)

When the line ::struct::matrix mat is transferred to global scope, then everything work OK.
Do you know how to make it work while the mat object is defined inside the method's body?

Edit: Forgot to mention - as can be seen from the code, I'm also using the CSV package!

Dor
  • 7,344
  • 4
  • 32
  • 45

1 Answers1

3

As Glenn Jackman said, it's a matter of namespace. If you call the following statements in the global scope:

set m [::struct::matrix mat]
puts "m is $m"               ;# ==> m is ::mat

However, within your class (I assume you use Itcl), then the namespace is a little different:

body className::methodName {args} {
    # ...
    set m [::struct::matrix mat]
    puts "m is $m"               ;# ==> m is ::className::mat

Instead of having to deal with namespaces, there is a different (if not better) way: use automatic naming:

body className::methodName {args} {
    # ...
    set mat [::struct::matrix]
    puts "mat is $mat"               ;# ==> mat is ::className::matrix1

From now on, you only have to deal with $mat, which works both within and outside of a class' scope. Of course, if you want to use $mat from another method, you need to save it as a class variable or pass from one method to another (for example, via return statement).

Hai Vu
  • 37,849
  • 11
  • 66
  • 93