print

The print statement is used to display text to standard output while a USE script is executing.

Syntax

print [-n]word|{buffer_name} [... word|{buffer_name]

Details

The print statement enables user-defined output to be generated during the execution of a USE script. When retrieving data from external sources it may take some time for a lengthy series of operations to complete, so one use of the print statement is to provide periodic status updates during this time.

The print statement will process as many arguments as it is given, but at least one argument is required. If the first argument is -n then no newline will be output after the last argument has been echoed to standard output, else a newline is output after the last argument.

Arguments that are normal words will be sent to standard output followed by a space. Arguments referencing a named buffer will result in the contents of the buffer being displayed.

Note that print will stop output of data from a named buffer as soon as a NUL (ASCII value 0) character is encountered

Binary data

It is not recommended that print is given a buffer containing binary data to display, as when echoed to a console on screen this is likely to result in various control codes and other sequences to be sent to the console which may have undesired side effects.

Printing variables that contain JSON data

It may be desirable to print the contents of a variable that contains a JSON string. This causes a potential issue insofar as JSON data is enclosed within curly braces - { ... } which results in the print statement interpreting that data as a named buffer.

In order to avoid this, simply prefix the variable name with something that is not an opening curly brace and surround the prefix and the variable name with double quotes as shown below:

var ID = Whatever
var http_body = "{\"ids\": [\"${ID}\"]}"
print "JSON: ${http_body}"

The prefix may also be a space as shown below:

var ID = Whatever
var http_body = "{\"ids\": [\"${ID}\"]}"
print " ${http_body}"

Example

var server = "https://my_json_server.com"
print Obtaining token from server
buffer response = http GET ${server}/generatetoken
print Token received:
print {response}
# Create a variable called ${secret_token} from the
# 'access_token' string in the JSON in the {response} buffer
var secret_token = $JSON{response}.[access_token]
# We no longer need the {response} buffer as the value
# extracted from it is stored in a variable
discard {response}
print Original server response now discarded