PDL::Options(3pm)

πŸ’– PDL::Options πŸ’–

okay so this is the PDL::Options(3pm) page from the User Contributed Perl Documentation collection, bestie β€” and it's literally all about making option-passing by hash in PerlDL not a whole nightmare. let's get into it 🫢


✨ NAME ✨

PDL::Options β€” simplifies option passing by hash in PerlDL. periodt. πŸ’…


πŸ¦‹ SYNOPSIS πŸ¦‹

use PDL::Options;

%hash = parse( \%defaults, \%user_options);

use PDL::Options ();

$opt = PDL::Options->new;
$opt = PDL::Options->new( \%defaults );

$opt->defaults ( \%defaults );
$opt->synonyms ( { 'COLOR' => 'COLOUR' } );

$hashref = $opt->defaults;

$opt->options ( \%user_options );

$hashref = $opt->options;

$opt->incremental(1);
$opt->full_options(0);

look at her go, she's doing so much πŸŽ€


🌸 DESCRIPTION 🌸

It's an object, right, and its whole job is simplifying option passing for PerlDL subroutines. Basically it lets you merge a user defined options with defaults. And because not everyone wants to do the OO thing, there's also a simplified (non-OO) interface provided. versatile queen πŸ‘‘

✨ Utility functions ✨

πŸŽ€ ifhref πŸŽ€

parse({Ext => 'TIF', ifhref($opt)});

okay so this one is so simple it's iconic: it just returns the argument if it's a hashref, otherwise it returns an empty hashref. that's it. that's the function. πŸ™ˆ

It's lowkey useful in conjunction with parse β€” it lets you return just the default values if the argument isn't a hash ref. chef's kiss πŸ“


πŸ’… NON-OO INTERFACE πŸ’…

A simplified non-object oriented interface is provided. These routines are exported into the callers namespace by default. no -> required, we're keeping it casual ✨

  • parse( \%defaults, \%user_options ) β€” this will parse user options by using the defaults. The following settings are used for parsing: the options are case-sensitive, a default synonym table is consulted (see "Default Synonyms"), minimum-matching is turned on, and translation of values is not performed.

A hash (not hash reference!!) containing the processed options is returned, bestie, don't forget that part 😭

%options = parse( { LINE => 1, COLOUR => 'red'}, { COLOR => 'blue'});
  • iparse( \%defaults, \%user_options ) β€” same as parse but matching is case insensitive πŸ¦‹

🌈 Default Synonyms 🌈

The following default synonyms are available in the non-OO interface:

COLOR  => COLOUR
COLOUR => COLOR
CENTER => CENTRE
CENTRE => CENTER

they're matching each other, it's giving mutual support 🫢


πŸ‘‘ METHODS πŸ‘‘

The following methods are available to PDL::Options objects. okay there's a lot of them, hydrate, we got this πŸ’œ

  • new() β€” constructor. Creates the object. With an optional argument it can also set the default options. glow-up from nothing to something ✨
  • extend (\%options) β€” this will copy the existing options object and extend it with the requested extra options. like getting a little sibling but for options πŸŽ€
  • defaults( \%defaults ) β€” method to set or return the current defaults. The argument should be a reference to a hash. The hash reference is returned if no arguments are supplied.
⚠️ heads up: the current values are reset whenever the defaults are changed. no cap.
  • add_synonym (\%synonyms) β€” method to add another synonym to an option set. The argument should be a reference to a hash.
  • add_translation (\%translation) β€” method to add another translation rule to an option set. The argument should be a reference to a hash.
  • synonyms( \%synonyms ) β€” method to set or return the current synonyms. The argument should be a reference to a hash. The hash reference is returned if no arguments are supplied.

This lets you provide alternate keywords β€” such as allowing 'COLOR' as an option when your defaults uses 'COLOUR'. so inclusive of everyone's spelling πŸ’–

  • current β€” returns the current state of the options. This comes back as a hash reference (although it is not a reference to the actual hash stored in the object β€” got it? it's a copy situation, not a share situation). If full_options() is true, the full options hash is returned; if full_options() is false, only the modified options are returned (as set by the last call to options()).
  • clear_current β€” this routine clears the 'state' of the PDL::Options object so that the next call to current will return an empty list. factory reset but cute 🧚
  • translation β€” provide translation of options to more specific values that are recognised by the program. This allows, for example, the automatic translation of the string 'red' to '#ff0000'. yes bestie, it speaks hex 🎨

This method can be used to setup the dictionary and is a hash reference with the following structure:

OPTIONA => {
            'string1' => decode1,
            'string2' => decode2
            },
OPTIONB => {
            's4' => decodeb1,
           }
etc....

Where OPTION? corresponds to the top level option name as stored in the defaults array (eg LINECOLOR) and the anonymous hashes provide the translation from string1 ('red') to decode1 ('#ff0000').

An options string will be translated automatically during the main options() processing if autotrans() is set to true. Else translation can be initiated by the user using the translate() method. iykyk πŸ’…

  • incremental β€” specifies whether the user defined options will be treated as additions to the current state of the object (1) or modifications to the default values only (0).

Can be used to set or return this value. Default is false.

  • full_options β€” governs whether a complete set of options is returned (ie defaults + expanded user options), true, or if just the expanded user options are returned, false (ie the values specified by the user).

This can be useful when you're only interested in the changes to the options rather than knowing the full state. (For example, if defaults contains keys for COLOUR and LINESTYLE and the user supplied a key of COL, you may simply be interested in the modification to COLOUR rather than the state of LINESTYLE and COLOUR.) yes, main character energy β€” only caring about what changed ✨

Default is true.

  • casesens β€” specifies whether the user defined options will be processed independent of case (0) or not (1). Default is to be case insensitive.

Can be used to set or return this value.

  • minmatch β€” specifies whether the user defined options will be minimum matched with the defaults (1) or whether the user defined options should match the default keys exactly. Defaults is true (1).

If a particular key matches exactly (within the constraints imposed bby case sensitivity) this key will always be taken as correct even if others are similar. For example COL would match COL and COLOUR but this implementation will always return COL in this case (note that for CO it will return both COL and COLOUR and pick one at random.) 😭

Can be used to set or return this value.

  • autotrans β€” specifies whether the user defined options will be processed via the translate() method immediately following the main options parsing. Default is to autotranslate (1).

Can be used to set or return this value.

  • casesenstrans β€” specifies whether the keys in the options hash will be matched insensitive of case (0) during translation() or not (1). Default is to be case insensitive.

Can be used to set or return this value.

  • minmatchtrans β€” specifies whether the keys in the options hash will be minimum matched during translation(). Default is false (0).

If a particular key matches exactly (within the constraints imposed bby case sensitivity) this key will always be taken as correct even if others are similar. For example COL would match COL and COLOUR but this implementation will always return COL in this case (note that for CO it will return both COL and COLOUR and pick one at random.) πŸ™ˆ

Can be used to set or return this value.

  • warnonmissing β€” turn on or off the warning message printed when an options is not in the options hash. This can be convenient when a user passes a set of options that has to be parsed by several different option objects down the line. lowkey essential for the multi-parser girlies πŸŽ€
  • debug β€” turn on or off debug messages. Default is off (0). Can be used to set or return this value.
  • options β€” takes a set of user-defined options (as a reference to a hash) and merges them with the current state (or the defaults; depends on the state of incremental()).

The user-supplied keys will be compared with the defaults. Case sensitivity and minimum matching can be configured using the mimatch() and casesens() methods.

A warning is raised if keys present in the user options are not present in the defaults unless warnonmissing is set.

A reference to a hash containing the merged options is returned.

$merged = $opt->options( { COL => 'red', Width => 1});

The state of the object can be retrieved after this by using the current() method or by using the options() method with no arguments. If full_options() is true, all options are returned (options plus overrides); if full_options() is false then only the modified options are returned.

Synonyms are supported if they have been configured via the synonyms() method. πŸ’–

  • translate β€” translate the current option values (eg those set via the options() method) using the provided translation().

This method updates the current state of the object and returns the updated options hash as a reference.

$ref = $opt->translate;

🌈 EXAMPLE 🌈

Two examples are shown. The first uses the simplified interface and the second uses the object-oriented interface. okay so this part is the glow-up tutorial ⭐

✨ Non-OO ✨

use PDL::Options (':Func');

%options = parse( {
                LINE => 1,
                COLOUR => 'red',
               },
               {
                COLOR => 'blue'
               }
             );

This will return a hash containing

%options = (
             LINE => 1,
             COLOUR => 'blue'
           )

the COLOR walked in and the COLOUR said "yes bestie" πŸ¦‹

✨ Object oriented ✨ (the main character arc)

okay bestie, this example is gonna walk you through the whole glow-up, step by step πŸ’…

use PDL::Options ();

# Create new object and supply defaults
$opt = PDL::Options->new(   { Colour => 'red',
                             LineStyle => 'dashed',
                             LineWidth => 1
                           }
                        );

# Create synonyms
$opt->synonyms( { Color => 'Colour' } );

# Create translation dictionary
$opt->translation( { Colour => {
                      'blue' => '#0000ff',
                      'red'  => '#ff0000',
                      'green'=> '#00ff00'
                             },
                     LineStyle => {
                      'solid' => 1,
                      'dashed' => 2,
                      'dotted' => 3
                      }
                   }
                 );

# Generate and parse test hash
$options = $opt->options( { Color => 'green',
                            lines => 'solid',
                           }
                        );

Run that cutie and $options is a reference to a hash holding literally this:

Colour => '#00ff00',
LineStyle => 1,
LineWidth => 1

But wait β€” plot twist 🌈 if full_options() was set to false (0), then $options would be a reference to a hash containing just:

Colour => '#00ff00',
LineStyle => 1

Minimum matching and case insensitivity can be configured for both the initial parsing and for the subsequent translating. And the translating? Yeah that can be turned off entirely if it's not the vibe. 🫢

ngl, currently synonyms are not available for the translation β€” even though this could be added quite simply. It's giving "coming soon to a patch near you" πŸ’€

πŸ‘‘ AUTHOR πŸ‘‘

Copyright (C) Tim Jenness 1998 (t.jenness@jach.hawaii.edu). All rights reserved. There is no warranty. You are allowed to redistribute this software / documentation under certain conditions. For details, see the file COPYING in the PDL distribution. If this file is separated from the PDL distribution, the copyright notice should be included in the file. πŸ“œβœ¨


and that's a wrap, bestie πŸŽ€ this was perl v5.42.3, dated 2026-08-20, and it ate, no crumbs left. PDL::Options(3pm) out πŸ¦‹πŸ’–

πŸ“¦ where this page comes from