You can use \global like this :
{\pgfmathsetlength{\global\testl}{5pt}}\the\testl
Some remarks : Egreg notices this code is very similar to
\setlength{\global\testl}{5pt}
It may be interesting to know what the macro pgfmathsetlength provides. In the file pgfmathcalc.code.tes we can read :
% #1 = dimension register
% #2 = expression
%
% Description:
%
% These functions work similar to \setlength and \addtolength. Only,
% they allow #2 to contain an expression, which is evaluated before
% assignment. Furthermore, the font is setup before the assignment is
% done, so that dimensions like 1em are evaluated correctly.
%
% If #2 starts with "+", then a simple assignment is done (but the
% font is still setup). This is orders of magnitude faster than a
% parsed assignment.
Another possibility to declare a length global is to redefine pgfmathsetlength but it's bad or to use a new macro :globalpgfmathsetlength based on pgfmathsetlength. We need to add \global in two lines but simplest is my first answer.
\makeatletter
\def\globalpgfmathsetlength#1#2{%
\expandafter\pgfmath@onquick#2\pgfmath@%
{%
% Ok, quick version:
\begingroup%
\pgfmath@selectfont%
\pgfmath@x#2\unskip%
\pgfmath@returnone\pgfmath@x%
\endgroup%
\global#1\pgfmathresult pt\relax% here add \global before #1
}%
{%
\pgfmathparse{#2}%
\global#1\pgfmathresult pt\relax% and here
}%
\ignorespaces%
}
.logfile say about using\global? – Werner Dec 20 '11 at 21:43\globaldoesn't issue an error; this is because the token that actually it acts on is\futurelet; it definitely doesn't act on the setting of\testtl's value, unfortunately. – egreg Dec 20 '11 at 21:46