beetlebug▾
/

ztcl::meter run

Runs a script in the current interpreter and counts it in a meter.

ztcl::meter run NAME SCRIPT

Description

Runs SCRIPT in the current interpreter, counts its use in NAME, and gives the result of SCRIPT. The budget of NAME caps the units of SCRIPT: at the end of the budget, SCRIPT gets the error compute limit reached, with -errorcode NONE, and the caller can catch it. An error of SCRIPT is the error of run.

Arguments

ArgumentTypeDefaultDescription
NAMEstringrequiredThe name of a meter.
SCRIPTscriptrequiredThe script to run and count.

Returns

The result of SCRIPT.

Examples

Each example of this page runs after package require ztcl::json.

% ztcl::meter create f
> ztcl::meter run f {expr {6 * 7}}
42

% ztcl::meter create loop
> ztcl::meter run loop {for {set i 0} {$i < 10} {incr i} {}}
> dict get [ztcl::meter get loop] units
22

% ztcl::meter create i -budget 10
> catch {ztcl::meter run i {while 1 {}}}
> dict get [ztcl::meter get i] left
0

Errors

% ztcl::meter create f
> ztcl::meter run f {error boom}
boom
-errorcode NONE

% ztcl::meter create none -budget 0
> ztcl::meter run none {set a 1}
compute limit reached
-errorcode NONE

% ztcl::meter create outer
> ztcl::meter run outer {ztcl::meter run outer {}}
the interpreter already counts in a meter
-errorcode ZTCL METER BUSY

% ztcl::meter run nope {}
no meter "nope"
-errorcode ZTCL METER NAME

% ztcl::meter run demo
wrong # args: should be "ztcl::meter run name script"
-errorcode TCL WRONGARGS

Facts

Capability
meter. A new interpreter does not have it until the host turns it on.
Tcl release
Tcl 9.1 and 8.6.
Since
ztcl 1.0.0-beta.1

See also