\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#207: Add a -namespace Option to [interp invokehidden]}
\date{June 29, 2004}
\author{Joe Mistachkin, Don Porter}
\urlstyle{sf}
\setlength{\parskip}{1ex}
\setlength{\parindent}{0pt}
\def\tipversion$#1${\texttt{\$#1\$}}
\def\tiplangle#1{\ensuremath{<}}
\def\tiprangle#1{\ensuremath{>}}
\def\tipbar#1{\ensuremath{|}}
\def\tipmail#1#2{\(\langle\){\small\expandafter\url{#1@#2}}\(\rangle\)}
\ifx\pdfoutput\undefined
\newcommand{\tipimage}[2]{\typeout{Make sure you download #1.eps}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.eps}}{\includegraphics[width=0.8\textwidth]{#1.eps}}}
\newcommand{\tipxref}[1]{}
\newcommand{\tipxrefend}{}
\else
\newcommand{\tipimage}[2]{\typeout{Make sure you create #1.pdf}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.pdf}}{\includegraphics[width=0.8\textwidth]{#1.pdf}}}
\pdfcatalog{/PageMode /UseOutlines}
\newcommand{\tipxref}[1]{\pdfannotlink attr {/C [0.5 0.5 1.0] /Border [0 0 1]} goto name {#1}}
\newcommand{\tipxrefend}{\pdfendlink}
\fi
\newenvironment{tipabstract}{\begin{abstract}}{\end{abstract}}
\begin{document}\maketitle
\begin{center}\begin{tabularx}{\linewidth}{|r@{: }X|}\hline
\textbf{TIP \#207}&\textbf{Add a -namespace Option to [interp invokehidden]}\\\hline
Author&

Joe Mistachkin \tipmail{joe}{mistachkin.com}
\par
Don Porter \tipmail{dgp}{users.sf.net}
\\
Created&Tuesday, \(\text{29}^{\text{th}}\) June 2004\\
Type&Project\\
Tcl Version&\(8.5\)\\
State&Final\\
Vote&Done\\
Version&\tipversion$Revision: 1.8 $\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This TIP proposes a \textit{-namespace} option for the \textbf{interp invokehidden} subcommand to allow hidden commands to be invoked in the specified namespace context in the slave interpreter.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Rationale}

Safe interpreters and namespaces have been useful and important features of Tcl since version 8.0. However, it can sometimes be difficult to use them together effectively. For example, while you can invoke a hidden command in a safe interpreter with \textbf{interp invokehidden}, such as \textbf{source}, you cannot specify the target namespace context where the command should be invoked. Using the \textbf{interp eval} subcommand is not an option because that, by design, does not allow hidden commands to be invoked. In addition, since the \textbf{interp invokehidden} subcommand expects a command name, \textbf{namespace eval} and similar constructs that would typically be used to change the target namespace context cannot be used.

\section{Specification}

\begin{quote}
\textbf{interp invokehidden} \textit{path} ?\textbf{-namespace} \textit{namespace}? ?\textbf{-global}? ?\textbf{-{}-}? \textit{hiddenCmdName} ?\textit{arg ...}?
\end{quote}

The \textbf{interp invokehidden} subcommand would continue to function as the current documentation dictates with the following notable exceptions:
\begin{itemize}
\item{}
The \textbf{-namespace} specifies the target namespace context in the slave interpreter where the command should be invoked starting from the global namespace in the slave interpreter.
\item{}
The target namespace context specified by the \textbf{-namespace} option is created in the slave interpreter if it does not already exist.
\item{}
If both the \textbf{-global} and \textbf{-namespace} options are present, the \textbf{-namespace} option is ignored.
\end{itemize}

A \textbf{-{}-} option (with the standard meaning) will be added as well.

The slave-interpreter command version of \textbf{invokehidden} will be updated in an equivalent way.

\subsection{Example Usage}

The following example scripts illustrates how this feature might be used.
\begingroup\small\begin{verbatim}
################################################
# file inside.tcl
################################################

set x "this is a test from inside [namespace current]."

################################################
# file invoke.tcl
################################################

set i [interp create -safe]
set file [file join [file dirname [info script]] inside.tcl]

interp invokehidden $i -namespace ::test::foo::bar source $file

set result [interp eval $i [list set ::test::foo::bar::x]]

# should be: "this is a test from inside ::test::foo::bar."
puts stdout $result
\end{verbatim}
\endgroup

\section{Proposed Internal C API Changes}

To be determined.

\section{Reference Implementation}

A reference implementation of this TIP is availble [\url{http://sf.net/tracker/?func=detail&aid=981841&group_id=10894&atid=310894}].

\section{Comments}

\textit{DGP}: Note that the \textbf{interp} command (including the \textbf{interp invokehidden} subcommand) was added for Tcl 7.5. The \textbf{namespace} command arrived later in Tcl 8.0. This proposal observes and proposes to correct that the \textbf{interp invokehidden} command was not updated to account for the arrival of namespaces. Clearly the \textit{-global} option indicates that control over evaluation context is something the caller needs. This command simply failed to evolve in a timely manner. This proposal is an overdue correction.

Looking ahead, I'd be happy if the \textit{-global} option was dropped from the documentation, so that only the new \textit{-namespace} option is favored, and so that the recommended way one forces evaluation in the global namespace is with \textit{-namespace ::}. The \textit{-global} option would still work through the remaining Tcl 8 releases, but no longer be recommended for new code.

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\end{document}
