X2Go::Server::Agent::NX::Options(3pm) User Contributed Perl Documentation X2Go::Server::Agent::NX::Options(3pm)

X2Go::Server::Agent::NX::Options - NX Options modification module

 use X2Go::Server::Agent::NX::Options;
 # Options string, probably read in from somewhere, but
 # hardcoded here.
 my $options = 'some=option,another=opt,more=values,some=override,more=data:90';
 # Parse into an intermediate form.
 my $intermediate = X2Go::Server::Agent::NX::Options::parse_options ($options);
 # Check for errors.
 die "Unable to parse option string, aborting.\n" unless (defined ($intermediate));
 # (Optionally) compact it, this should make the duplicated "some" and "more"
 # keys unique.
 $intermediate = X2Go::Server::Agent::NX::Options::compact_intermediate ($intermediate);
 # Error handling ...
 die "Unable to compact intermediate array, aborting.\n" unless (defined ($intermediate));
 # Add to options string.
 my $transform_op = '+new=value';
 # Parse and sanitize transform string.
 my $interpreted_transform_ref = X2Go::Server::Agent::NX::Options::interpret_transform ($transform_op);
 # Error handling ...
 die "Invalid transformation passed, aborting.\n" unless (defined ($interpreted_transform_ref));
 # Extract transformation data.
 my ($transform_mode, $sanitized_transform) = @{$interpreted_transform_ref};
 # Apply transformation.
 $intermediate = X2Go::Server::Agent::NX::Options::transform_intermediate ($intermediate, $transform_mode, $sanitized_transform);
 # Error handling ...
 die "Error while transforming intermediate representation, aborting.\n" unless (defined ($intermediate));
 # Try to remove a combination which doesn't exist, this should not modify the
 # intermediate.
 # No more comments for things that were already explained.
 $transform_op = '-another=doesnotexist';
 $interpreted_transform_ref = X2Go::Server::Agent::NX::Options::interpret_transform ($transform_op);
 die "Invalid transformation passed, aborting.\n" unless (defined ($interpreted_transform_ref));
 ($transform_mode, $sanitized_transform) = @{$interpreted_transform_ref};
 $intermediate = X2Go::Server::Agent::NX::Options::transform_intermediate ($intermediate, $transform_mode, $sanitized_transform);
 die "Error while transforming intermediate representation, aborting.\n" unless (defined ($intermediate));
 # Remove a key unconditionally, this should change the intermediate.
 $transform_op = '-some';
 $interpreted_transform_ref = X2Go::Server::Agent::NX::Options::interpret_transform ($transform_op);
 die "Invalid transformation passed, aborting.\n" unless (defined ($interpreted_transform_ref));
 ($transform_mode, $sanitized_transform) = @{$interpreted_transform_ref};
 $intermediate = X2Go::Server::Agent::NX::Options::transform_intermediate ($intermediate, $transform_mode, $sanitized_transform);
 die "Error while transforming intermediate representation, aborting.\n" unless (defined ($intermediate));
 # Modify/update a key.
 $transform_op = '+another=newval';
 $interpreted_transform_ref = X2Go::Server::Agent::NX::Options::interpret_transform ($transform_op);
 die "Invalid transformation passed, aborting.\n" unless (defined ($interpreted_transform_ref));
 ($transform_mode, $sanitized_transform) = @{$interpreted_transform_ref};
 $intermediate = X2Go::Server::Agent::NX::Options::transform_intermediate ($intermediate, $transform_mode, $sanitized_transform);
 die "Error while transforming intermediate representation, aborting.\n" unless (defined ($intermediate));
 # Extract the "more" key.
 my $extract = X2Go::Server::Agent::NX::Options::extract_element ($intermediate, q{more});
 # Error handling ...
 die "Unable to extract 'more' key from intermediate, aborting.\n" unless defined ($extract);
 # Fetching multiple elements could be fine, for instance when the intermediate is not compact.
 # Hence, this need not be a general error, but we'll treat it as one here.
 die "Extract operation returned more than one element, this should not happen with a compacted intermediate, aborting.\n" if (1 < scalar (@{$extract}));
 # Likewise, it would be okay for the result to be empty, but not for us right here.
 die "Extract operation returned no result, aborting.\n" if (1 > scalar (@{$extract}));
 my $extracted_kv = q{};
 # Now, get the actual data in a presentable form.
 foreach my $entry (@{$extract}) {
   foreach my $key (%{$entry}) {
     $extracted_kv .= $key;
     my $value = $entry->{$key};
     if (defined ($value)) {
       $extracted_kv .= q{=} . $value;
     }
     last;
   }
   last;
 }
 # At this point, $extracted_kv should be "more=data".
 # Yet again, but this time extracting a key which does not exist.
 $extract =  X2Go::Server::Agent::NX::Options::extract_element ($intermediate, q{nosuchey});
 # Error handling ...
 die "Unable to extract 'nosuchkey' key from intermediate, aborting.\n" unless defined ($extract);
 # Should be empty.
 die "Extract operation returned a result, aborting.\n" if (0 < scalar (@{$extract}));
 # Transform back into a string.
 my $out = X2Go::Server::Agent::NX::Options::intermediate_to_string ($intermediate);
 # Error handling ...
 die "Unable to transform intermediate back into string, aborting.\n" unless (defined ($out));
 # At this point, $out should be 'another=newval,more=data,new=value:90'.

Use this module to modify or extract data from X2Go/NX Agent options strings. Refer to "OPTIONS STRINGS" for an in-depth description of options strings.

First, transform the input options string into an intermediate representation via "parse_options". The options string must end with a display specification (i.e., it must end in ":displaynumber"). Parsing errors are indicated by it returning "undef". The returned value is actually a reference to an array of hash references, but you should make no assumptions to the layout or even its actual format. Treat it like a black box. Crucially, whenever an intermediate is expected, such a reference should be passed.

To remove redundant or empty entries within an options string, pass the intermediate to "compact_intermediate". This is entirely optional and can be done at any step, as long as an intermediate is available. If you intend to extract data, it is recommended, but not necessary, to compact the intermediate first.

In order to extract key-value pairs, supply the intermediate and a key-value pair to extract to "extract_element". Its result will be either undefined on error, or a reference to an array consisting of references to hashes. Each hash contains exactly one key-value pair.

The same approach using "extract_element" can be used to check for the existence of either keys or full key-value pairs. If the returned array is empty, no such element exists.

For compacted intermediates, each extraction operation returns an array with at most one hash reference entry. Non-compacted intermediates can contain a key multiple times, so no guarantee regarding the result's magnitude can be given.

To parse transformations, pass each one to "interpret_transform". Refer to "TRANSFORMATIONS" for documentation on transformation formats. This will either return "undef" on error, or an array of two scalars - the transformation mode (an internal number) and the sanitized transformation string (i.e., the original transformation string with any preceding operator removed).

Pass the intermediate, the transformation mode and the sanitized transformation string to "transform_intermediate" to modify the intermediate value.

Repeat this until the intermediate is modified to your liking.

Finally, pass the intermediate to "intermediate_to_string" in order to parse it back into a normal string. This operation is essentially the opposite of "parse_options". As usual, "undef" is returned on error.

Generally, parsing an options string to an intermediate via "parse_options" and then immediately parsing it back into a string via "intermediate_to_string" should always produce an options string that is identical to the original options string (assuming no errors occurred).

If this is not the case, please report a bug.

Any subroutines and constants not marked as exportable are explicitly not documented and should be regarded as internal and not be used. There is no guarantee regarding their behavior or existence.

X2Go/NX Agent options strings are fully documented in nxagent's documentation and additional, linked places therein.

This module is not really concerned with the actual content of an options string, but mostly its format.

An options string follows the form [[key[=value,]]:displaynumber.

This has some interesting implications:

Transformations follow the form [+]|-key[=value], which means that:

This manual has been written by Mihai Moldovan <ionic@ionic.de> <mailto:ionic@ionic.de> for the X2Go project (<https://www.x2go.org>).

2024-04-22 perl v5.38.2