Data types GET, POST and ENV

P* provides three data types which implicitly retrieve data from the environment they run in, usually the web server. The data is parsed and stored the first time you use one of them.

GET

The GET-type reads the environment variables QUERY_STRING, which contains the query string from the user like ?id=45. The GET-type is accessed just like a hash, but if you access an element which was not defined in the query string, the value will be boolean false.

You can use the defined operator to check if a value is set or not.

If P* doesn't find the QUERY_STRING environment variable, it will crash your program, but an empty query string is OK. You may have multiple GET-variables in your program, but P* will parse the query string once for every instance.

Here is an example of usage of the GET-type.

SCENE main {
	/*
	   Before running this test, you need to set a query string in the terminal manually like this:
	   export QUERY_STRING="a&b=B+is+a+string+value&d=First+D&d=Second+D"
	 */

	/* Instantiate the get object */
	GET get;

	/* Create an array of keys to look for */
	array<string> keys_to_check;
	keys_to_check[0] = "a";
	keys_to_check[1] = "b";
	keys_to_check[2] = "c";
	keys_to_check[3] = "d";

	/* Iterate the array of keys and check the keys in the GET object */
	int i = 0;
	while (i < @keys_to_check) {
		string key = keys_to_check[i];

		/* Check if the key is defined */
		if (defined get[key]) {
			echo "The key '$key' is set in the query string\n";
			echo "\tThere are " . @get[key] . " instances of this key\n";
			if (@get[key] > 1) {
				int j = 0;
				while (j < @get[key]) {
					get[key][j];
					echo "\tThe value of '$key' instance number $j is '" . get[key][j] . "'\n";
					j++;
				}
			}
			else {
				echo "\tThe only value of '$key' is '" . *get[key] . "'\n";
			}
		}
		else {
			echo "Could not find key '$key' in the query string\n";
		}
		echo "\n";
		i++;
	}
}

This program will output

The key 'a' is set in the query string
There are 1 instances of this key
The only value of 'a' is 'true'

The key 'b' is set in the query string
There are 1 instances of this key
The only value of 'b' is 'B is a string value'

Could not find key 'c' in the query string

The key 'd' is set in the query string
There are 2 instances of this key
The value of 'd' instance number 0 is 'First D'
The value of 'd' instance number 1 is 'Second D'

POST

The POST-type is used exactly the same way as GET, but since it reads from standard intput, you can only have one instance. You can use both POST and GET at the same time.

ENV

The ENV-type provides the element selection operator -> to access different environment variables. Variables are not cached, and getenv(3) is used to read environment variables every time you access them. P* will crash your program if you access a variable which doesn't exist. To avoid this, you should use the defined-operator to check for existence of variables.

Example with the ENV type:

SCENE main {
	ENV env;
	echo "The current path is '" . env->PATH . "'\n";
}

This program will output something like

The current path is '/usr/local/bin:/usr/bin:/bin:/usr/test/bin/'