# latex3 clist put inside an environment definition

I'm puzzled as to why the following doesn't work:

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn
\clist_new:N \l_test
\clist_set:Nn \l_test {a,b,c}
\NewDocumentCommand{\putitem}{m}{
\clist_put_right:Nn \l_test {#1}}
\NewDocumentEnvironment{foobar}{mm}{ FOO }{ BAR \putitem{##1}}
\NewDocumentCommand{\giveitems}{}{\clist_map_inline:Nn \l_test {\fbox{##1}}}
\ExplSyntaxOff

\begin{document}

\putitem{xyz}
\putitem{asdf}
\giveitems

\begin{foobar}{abab}{baba}
XXX
\end{foobar}

\giveitems % output is the same as the earlier call
\end{document}


So as part of making the environment, an additional string should be added to the clist. But it doesn't work. I would expect the second \giveitems to have an additional item, namely abab. Replacing \putitem with its definition inside the new environment doesn't make any difference. What am I missing here?

Environments form groups, which means you'll need a global variable to see the result outside. At the same time, you need #1 not ##1 when using the argument from your environment.

\ExplSyntaxOn
\clist_new:N \g__seamus_test_clist
\clist_set:Nn \g__seamus_test_clist {a,b,c}
\NewDocumentCommand{\putitem}{m}{
\clist_gput_right:Nn \g__seamus_test_clist {#1}
}
\NewDocumentEnvironment{foobar}{mm}
{ FOO }
{ BAR \putitem{#1}}
\NewDocumentCommand{\giveitems}{}
{\clist_map_inline:Nn \g__seamus_test_clist {\fbox{##1}}}
\ExplSyntaxOff

• I've tided up the variable name here to follow standard expl3 conventions Apr 30 at 15:00
• Thanks for the quick answer! what convention requires double underscores after the g in the clist? Apr 30 at 15:09
• @Seamus Double underscores mean “private variable”, so not to be used by third parties. Apr 30 at 15:35