Gnuplot::Builder::JoinDict - immutable ordered hash that joins its values in stringification
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
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:
Gnuplot::Builder::JoinDict is immutable. Every setter method doesn't alter the original object, but returns a new one.
When a Gnuplot::Builder::JoinDict object is stringified, it joins all its values with the given separator and returns the result.
Alias for Gnuplot::Builder::JoinDict->new(separator => $separator, content => \@content). Exported only by request.
Gnuplot::Builder::JoinDict->new(separator => $separator, content => \@content)
The constructor.
Fields in %args are:
%args
separator
The separator string that is used when joining.
content
The content of the $dict. The array-ref must contain key-value pairs. Keys must not be undef.
$dict
undef
filter
If set, this code-ref is called when the $dict is stringified (i.e. $dict->to_string is called). The code-ref is supposed to modify the values in $dict to produce the final result of stringification.
$dict->to_string
@modified_values = $filter->($dict)
where $dict is the Gnuplot::Builder::JoinDict object. The filter must return a list @modified_values.
@modified_values
For example,
my $dict = Gnuplot::Builder::JoinDict->new( separator => " & ", content => [x => 10, y => 20], filter => sub { my ($dict) = @_; my @keys = $dict->get_all_keys(); my @values = $dict->get_all_values(); return map { "$keys[$_]=$values[$_]" } 0 .. $#keys; } ); "$dict"; ## => x=10 & y=20
The filter is inherited by Gnuplot::Builder::JoinDict objects derived from the original one.
validator
If set, this code-ref is called when some key-value pairs are set or deleted to a Gnuplot::Builder::JoinDict object. The code-ref is supposed to check the content and throw an exception when something is wrong.
$validator->($dict)
where $dict is the Gnuplot::Builder::JoinDict object.
The validator is called when new(), delete(), set() and set_all() methods are called.
new()
delete()
set()
set_all()
The validator is inherited by Gnuplot::Builder::JoinDict objects derived from the original one.
Join $dict's values with the separator and return the result.
If some values are undef, those values are ignored.
Return the $value for the $key.
$value
$key
If $dict doesn't have $key, it returns undef.
Return all keys from $dict.
Return all values from $dict.
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
Set all values to $value and return the result.
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.
Create and return a clone of $dict.
Get the separator.
When you evaluate a $dict as a string, it executes $dict->to_string(). That is,
$dict->to_string()
"$dict" eq $dict->to_string;
Gnuplot::Builder::JoinDict implements Lens() method, so you can use Data::Focus to access its attributes.
Lens()
The Lens() method creates a Data::Focus::Lens object for accessing the content via get() and set() methods.
get()
use Data::Focus qw(focus); my $scalar = focus($dict)->get("x"); ## same as: my $scalar = $dict->get("x"); my $new_dict = focus($dict)->set(x => '($1 * 1000)'); ## same as: my $new_dict = $dict->set(x => '($1 * 1000)');
Toshio Ito, toshioito at cpan.org
toshioito at cpan.org
To install Gnuplot::Builder, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Gnuplot::Builder
CPAN shell
perl -MCPAN -e shell install Gnuplot::Builder
For more information on module installation, please visit the detailed CPAN module installation guide.