The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Gnuplot::Builder::JoinDict - immutable ordered hash that joins its values in stringification

SYNOPSIS

    use Gnuplot::Builder::JoinDict;
    
    my $dict = Gnuplot::Builder::JoinDict->new(
        separator => ', ',
        content => [x => 640, y => 480]
    );
    "$dict";  ## => 640, 480
    
    $dict->get("x"); ## => 640
    $dict->get("y"); ## => 480
    
    my $dict2 = $dict->set(y => 16);
    "$dict";   ## => 640, 480
    "$dict2";  ## => 640, 16
    
    my $dict3 = $dict2->set(x => 8, z => 32);
    "$dict3";  ## => 8, 16, 32
    
    my $dict4 = $dict3->delete("x", "y");
    "$dict4";  ## => 32

DESCRIPTION

Basically Gnuplot::Builder::JoinDict is just an ordered associative array (sometimes called as a "dictionary"), so it's the same as Tie::IxHash.

The difference from Tie::IxHash is:

CLASS METHODS

$dict = Gnuplot::Builder::JoinDict->new(%args)

The constructor.

Fields in %args are:

separator => STR (optional, default: "")

The separator string that is used when joining.

content => ARRAY_REF (optional, default: [])

The content of the $dict. The array-ref must contain key-value pairs. Keys must not be undef.

OBJECT METHODS

$str = $dict->to_string()

Join $dict's values with the separator and return the result.

If some values are undef, those values are ignored.

$value = $dict->get($key)

Return the $value for the $key.

If $dict doesn't have $key, it returns undef.

$new_dict = $dict->set($key => $value, ...)

Add new key-value pairs to $dict and return the result. You can specify more than one key-value pairs.

If $dict already has $key, its value is replaced in $new_dict. Otherwise, a new pair of $key and $value is added.

$new_dict = $dict->delete($key, ...)

Delete the given keys from $dict and return the result. You can specify more than one $keys.

If $dict doesn't have $key, it's just ignored.

$new_dict = $dict->clone()

Create and return a clone of $dict.

OVERLOAD

When you evaluate a $dict as a string, it executes $dict->to_string(). That is,

    "$dict" eq $dict->to_string;

AUTHOR

Toshio Ito, debug.ito [at] gmail.com