with-path

dbohdan 2015-03-11: with-path is to cd what withOpenFile is to open.

proc with-path {path code} {
    set prevPath [pwd]
    cd $path
    uplevel 1 $code
    cd $prevPath
}

DKF: In 8.6, it might be better to do:

proc with-path {path code} {
    set prevPath [pwd]
    cd $path
    try {
        uplevel 1 $code
    } finally {
        cd $prevPath
    }
}

AMG: No question about it, this is clearly a case where try ... finally ... is needed. This way the previous working directory is restored no matter what, plus errors generated by $code are propagated.

Additionally, DKF's version corrects a problem with the original: it ensures [with-path] returns the result of evaluating $code.