Previous chapter To contents Next chapter

Chapter 7, Miscellaneous functions

There are some 'functions' in Pike that are not really functions at all but just as builtin as operators. These special functions can do things that no other functions can do, but they can not be re-defined or overloaded. In this chapter I will describe these functions and why they are implemented as special functions.

7.1 sscanf

Sscanf may look exactly like a normal function, but normal functions can not set the variables you send to it. The purpose of sscanf is to match one string against a format string and place the matching results into a list of variables. The syntax looks like this:
int sscanf(string str, string fmt, lvalue ...)
The string str will be matched against the format string fmt. fmt can contain strings separated by %d,%s,%c and %f. Every % corresponds to one lvalue. An lvalue is the name of a variable, a name of a local variable, an index in an array, mapping or object. It is because of these lvalues that sscanf can not be implemented as a normal function.

Whenever a percent is found in the format string, a match is according to the following table:
%b reads a binary integer
%d reads a decimal integer
%o reads an octal integer
%x reads a hexadecimal integer
%D reads an integer that is either octal (leading zero), hexadecimal (leading 0x) or decimal.
%f reads a float
%c matches one char and returns it as an integer
%2c matches two chars and returns them as an integer (short)
%4F matches four chars and returns them as a float (IEEE single precision)
%8F matches eigth chars and returns them as a float (IEEE double precision)
%s reads a string. If followed by %d, %s will read any non-numerical characters. If followed by a %[], %s will read any characters not present in the set. If followed by normal text, %s will match all characters up to but not including the first occurrence of that text.
%5s gives a string of 5 characters (5 can be any number)
%[set] matches a string containing a given set of characters (those given inside the brackets). %[^set] means any character except those inside brackets. Example: %[0-9H] means any number or 'H'.
%{format%} Repeatedly matches 'format' as many times as possible and assigns an array of arrays with the results to the lvalue.

If a * is put between the percent and the operator, the operator will only match its argument, not assign any variables.

Sscanf does not use backtracking. Sscanf simply looks at the format string up to the next % and tries to match that with the string. It then proceeds to look at the next part. If a part does not match, sscanf immediately returns how many % were matched. If this happens, the lvalues for % that were not matched will not be changed.

Let's look at a couple of examples:

// a will be assigned "oo" and 1 will be returned
sscanf("foo","f%s",a);

// a will be 4711 and b will be "bar", 2 will be returned
sscanf("4711bar","%d%s",a,b);

// a will become "test"
sscanf(" \t test","%*[ \t]%s",a)

// Remove "the " from the beginning of a string
// If 'str' does not begin with "the " it will not be changed
sscanf(str,"the %s",str);

SEE ALSO : sprintf

7.2 catch & throw

Catch is used to trap errors and other exceptions in Pike. It works by making a block of code into an expression, like this:
catch { statements }
If an error occurs, catch will return a description of the error. The description of the error has the following format:
({
     "error description",
     backtrace()
})
If no error occurs, catch will return zero. You may emulate your own errors using the function throw, described in
chapter 16 "Builtin functions".

Example:

int x,y;
// This might generate "division by zero"
mixed error=catch { x/=y; };

7.3 gauge

The syntax for gauge is the same as the syntax for catch:
gauge { statements }
However, gauge simply returns how many seconds the code took to execute. This can be used to find out how fast your code actually is.. :) Only CPU time used by the Pike process is measured. This means that if it takes two seconds to execute but only uses 50 % CPU, this function will return 1.0.

7.4 typeof

This function returns the type of an expression as a string. It does not evaluate the expression at all, which might be somewhat confusing. Example:
typeof( exit(1) )
This will return the string "void" since exit is a function that returns void. It will not execute the function exit and exit the process as you might expect.

If you want to know the type after evaluation, use sprintf("%t", expr).


Previous chapter To contents Next chapter