++ed by:

1 PAUSE user
1 non-PAUSE user.

Andreas K. Hüttel
and 4 contributors


Lab::SCPI - Match SCPI headers and parameters against keywords.

This module exports a single function:

scpi_match($header, $keyword)

Return true, if $header matches the SCPI keyword expression $keyword.


The calls

 scpi_match($header, 'voltage[:APERture]')
 scpi_match($header, 'voltage|CURRENT|resistance')
 scpi_match($header, '[:abcdef]:ghi[:jkl]')

are convenient replacements for

 $header =~ /^(voltage:aperture|voltage:aper|voltage|volt:aperture|volt:aper|volt)$/i
 $header =~ /^(voltage|volt|current|curr|resistance|res)$/i
 $header =~ /^(:abcdef:ghi:jkl|:abcdef:ghi|:abcd:ghi:jkl|:abcd:ghi|:ghi:jkl|:ghi)$/i


Leading and trailing whitespace is removed from the first argument, before matching against the keyword.

Keyword Structure

See Sec. 6 "Program Headers" in the SCPI spec. Always give the long form of a keyword; the short form will be derived automatically. The colon is optional for the first mnemonic. There must be at least one non-optional mnemonic in the keyword.

scpi_match will throw, if it is given an invalid keyword.


returns the "short form" of the input keyword, according to the SCPI spec. Note that the keyword can have an appended number, that needs to be preserved: sweep1 -> SWE1. Any trailing '?' is also preserved, which is useful for general SCPI parsing purposes.

BEWARE: some instruments have ambivalant 'shortform' when constructed using normal rules: (Tektronix DPO4104 ACQUIRE:NUMENV and ACQUIRE:NUMAVG) you have to be aware of the mnemonic heirarchy for this, so "scpi_canon" has a way to deal with such special cases.

"Common" keywords (that start with '*') are returned unchanged.

SCPI 6.2.1: The short form mnemonic is usually the first four characters of the long form command header. The exception to this is when the long form consists of more than four characters and the fourth character is a vowel. In such cases, the vowel is dropped and the short form becomes the first three characters of the long form.

Got to watch out for that "usually". See scpi_canon for how to handle the more general case.

scpi_parse(string [,hash])

$hash = scpi_parse(string [,hash]) parse scpi command or response string, create a tree structure with hash keys for the mnemonic components, entries for the values.

example $string = ":Source:Voltage:A 3.0 V;B 2.7V;:Source:Average ON" results in $hash{Source}->{Voltage}->{A}->{_VALUE} = '3.0 V' $hash{Source}->{Voltage}->{B}->{_VALUE} = '2.7V' $hash{Source}->{Average}->{_VALUE} = 'ON'

If a hash is given as a parameter of the call, the information parsed from the string is combined with the input hash.

arrayref = scpi_parse_sequence(string[,arrayref])

returns an array of hashes, each hash is a tree structure corresponding to a single scpi command (like scpi_parse) Useful for when the sequence of commands is significant.

If an arrayref is passed in, the parsed string results are appended as new entries.

$canonhash = scpi_canon($hash[,$overridehash])

revise a hash tree of scpi mnemonics to use the 'shorter' forms, in uppercase

The "override" hash has the same form as the mnemonic hash (but with no _VALUE leaves on the tree), but each key is in the form 'MESSage' where uppercase is the shorter form. This is to allow shortening of mnemonics where the normal shortening rules don't work.

$flat = scpi_flat($thing[,$override])

convert the tree structure to a 'flat' key space: h->{a}->{b}->{INPUT3} -> f{A:B:INP3}, canonicalizing the keys This is useful for comparing values between two hash structures

if $thing = hash ref -> flat is corresponding hash if $thing = array ref -> flat is an array ref to flat hashes