/usr/lib/perl5/PDL/Options.pm is in pdl 1:2.4.7+dfsg-2ubuntu5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 | package PDL::Options;
=head1 NAME
PDL::Options - simplifies option passing by hash in PerlDL
=head1 SYNOPSIS
use PDL::Options;
%hash = parse( \%defaults, \%user_options);
use PDL::Options ();
$opt = new PDL::Options;
$opt = new PDL::Options ( \%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);
=head1 DESCRIPTION
Object to simplify option passing for PerlDL subroutines.
Allows you to merge a user defined options with defaults.
A simplified (non-OO) interface is provided.
=cut
use strict;
use Carp;
use vars qw/$VERSION %EXPORT_TAGS %DEF_SYNS @ISA/;
require Exporter;
# difference to 0.91 is that added CENTRE/CENTER as default
# synonymns (patch by Diab Jerius [ #469110 ])
$VERSION = '0.92';
@ISA = qw(Exporter);
%EXPORT_TAGS = (
'Func' => [qw/
parse iparse ifhref
/]
);
Exporter::export_tags('Func');
# List of default synonyms
%DEF_SYNS = (
COLOR => 'COLOUR',
COLOUR => 'COLOR',
CENTER => 'CENTRE',
CENTRE => 'CENTER',
);
my $default = {
WarnOnMissing => 1,
FullOptions => 1,
DEBUG => 0,
};
=head1 Utility functions
=head2 ifhref
parse({Ext => 'TIF', ifhref($opt)});
just return the argument if it is a hashref otherwise return
an empty hashref. Useful in conjunction with parse to return
just the default values if argument is not a hash ref
=head1 NON-OO INTERFACE
A simplified non-object oriented interface is provided.
These routines are exported into the callers namespace by default.
=over 4
=item 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 L</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.
%options = parse( { LINE => 1, COLOUR => 'red'}, { COLOR => 'blue'});
=item iparse( \%defaults, \%user_options)
Same as C<parse> but matching is case insensitive
=cut
sub ifhref {
my ($href) = @_;
return defined $href && ref $href eq 'HASH' ? $href : {};
}
sub parse { return _parse(1,@_) }
sub iparse { return _parse(0,@_) }
sub _parse {
croak 'Usage: parse( \%defaults, \%user )' if scalar(@_) != 3;
my $casechk = shift;
my $defaults = shift;
croak ("First argument is not a hash reference")
unless ref($defaults) eq "HASH";
my $user = shift;
croak ("Second argument is not a hash reference")
unless ref($user) eq "HASH";
# Create new object
my $opt = new PDL::Options ( $defaults );
# Set up default behaviour
$opt->minmatch(1);
$opt->casesens($casechk);
$opt->synonyms( \%DEF_SYNS );
# Process the options
my $optref = $opt->options( $user );
return %$optref;
}
=back
=head2 Default Synonyms
The following default synonyms are available in the non-OO interface:
COLOR => COLOUR
COLOUR => COLOR
CENTER => CENTRE
CENTRE => CENTER
=head1 METHODS
The following methods are available to PDL::Options objects.
=over 4
=item new()
Constructor. Creates the object. With an optional argument can also
set the default options.
=cut
sub new {
my $proto = shift;
my $class = ref($proto) || $proto;
my $opt = {};
# Set up object structure
$opt->{DEFAULTS} = {}; # Default options
$opt->{CURRENT} = {}; # Current options
$opt->{CurrKeys} = []; # list of selected keys if full_options(0)
$opt->{SYNONYMS} = {}; # List of synonyms
$opt->{INC} = 0; # Flag to decide whether we are incremental on cur
$opt->{CaseSens} = 0; # Are options case sensitive
$opt->{MinMatch} = 1; # Minimum matching on keys
$opt->{Translation} = {};# Translation from eg 'RED' to 1
$opt->{AutoTranslate}= 1;# Automatically translate options when processing
$opt->{MinMatchTrans} = 0; # Min matching during translation
$opt->{CaseSensTrans} = 0; # Case sensitive during translation
# Return full options list
$opt->{FullOptions} = $default->{FullOptions};
# Whether to warn for options that are invalid or not
$opt->{WarnOnMissing}= $default->{WarnOnMissing};
$opt->{DEBUG} = $default->{DEBUG}; # Turn on debug messages
# Bless into class
bless ( $opt, $class);
# If we were passed arguments, pass to defaults method
if (@_) { $opt->defaults( @_ ); }
return $opt;
}
=item extend (\%options)
This will copy the existing options object and extend it with the
requested extra options.
=cut
sub extend {
my ($self, $opt)=@_;
my $class = ref($self);
my $h = {%{$self}};
croak ("Argument is not reference to hash!\n") unless ref($opt) eq 'HASH';
#
# The next step is to perform a deep copy of the hash
# references since we might want to change these without
# changing the originals.
#
$h->{SYNONYMS}={%{$self->{SYNONYMS}}};
$h->{Translation}={%{$self->{Translation}}};
$h->{CurrKeys}=[@{$self->{CurrKeys}}];
#
# Create the extended option list.
#
my %all_options = (%{$opt}, %{$self->{DEFAULTS}});
# Bless it
bless ($h, $class);
# And parse the default options
$h->defaults(\%all_options);
return $h;
}
# =item change_defaults (\%options)
# This will merge the options given with the defaults hash and hence change
# the default hash. This is not normally a good idea, but in certain dynamic
# situations you might want to adjust a default parameter for future calls
# to the routine.
# =cut
# sub change_defaults {
# my $self=shift;
# my $arg = shift;
# croak("Argument is not a hash reference!\n") unless ref($arg) eq 'HASH';
# my $defs = $self->defaults($arg);
# $self->defaults($)
# }
=item 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.
The current values are reset whenever the defaults are changed.
=cut
sub defaults {
my $self = shift;
if (@_) {
my $arg = shift;
croak("Argument is not a hash reference") unless ref($arg) eq "HASH";
$self->{DEFAULTS} = $arg;
# Reset the current state (making sure that I disconnect the
# hashes
my %hash = %$arg;
$self->curr_full(\%hash);
}
# Decouple the hash to protect it from being modified outside the
# object
my %hash = %{$self->{DEFAULTS}};
return \%hash;
}
=item add_synonym (\%synonyms)
Method to add another synonym to an option set
The argument should be a reference to a hash.
=cut
sub add_synonym {
my $self=shift;
return unless @_;
my $arg = shift;
croak("Synonym argument is not a hash reference") unless ref($arg) eq "HASH";
foreach (keys %$arg) {
$self->{SYNONYMS}{$_}=$arg->{$_};
}
my %hash = %{$self->{SYNONYMS}};
return \%hash;
}
=item add_translation (\%translation)
Method to add another translation rule to an option set.
The argument should be a reference to a hash.
=cut
sub add_translation {
my $self = shift;
return unless @_;
my $arg = shift;
croak("Translation argument is not a hash reference") unless ref($arg) eq 'HASH';
foreach (keys %$arg) {
$self->{Translation}{$_}=$arg->{$_};
}
my %hash = %{$self->{Translation}};
return \%hash;
}
=item 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 allows you to provide alternate keywords (such as allowing
'COLOR' as an option when your defaults uses 'COLOUR').
=cut
sub synonyms {
my $self = shift;
if (@_) {
my $arg = shift;
croak("Argument is not a hash reference") unless ref($arg) eq "HASH";
$self->{SYNONYMS} = $arg;
}
# Decouple the hash to protect it from being modified outside the
# object
my %hash = %{$self->{SYNONYMS}};
return \%hash;
}
=item current
Returns the current state of the options. This is returned
as a hash reference (although it is not a reference to the
actual hash stored in the object). 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()).
=cut
sub current {
my $self = shift;
if ($self->full_options) {
return $self->curr_full;
} else {
my @keys = $self->curr_keys;
my %hash = ();
my $curr = $self->curr_full;
foreach my $key (@keys) {
$hash{$key} = $$curr{$key} if exists $$curr{$key};
}
return \%hash;
}
}
=item clear_current
This routine clears the 'state' of the C<PDL::Options> object so that
the next call to current will return an empty list
=cut
sub clear_current {
my $self = shift;
@{$self->{CurrKeys}}=();
}
# Method to set the 'mini' state of the object
# This is just a list of the keys in %defaults that were selected
# by the user. current() returns the hash with these keys if
# called with full_options(0).
# Not publicising this
sub curr_keys {
my $self = shift;
if (@_) { @{$self->{CurrKeys}} = @_; }
return @{$self->{CurrKeys}};
}
# Method to set the full state of the object
# Not publicising this
sub curr_full {
my $self = shift;
if (@_) {
my $arg = shift;
croak("Argument is not a hash reference") unless ref($arg) eq "HASH";
$self->{CURRENT} = $arg;
}
# Decouple the hash
my %hash = %{$self->{CURRENT}};
return \%hash;
}
=item 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'.
This method can be used to setup the dictionary and is 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.
=cut
sub translation {
my $self = shift;
if (@_) {
my $arg = shift;
croak("Argument is not a hash reference") unless ref($arg) eq "HASH";
$self->{Translation} = $arg;
}
# Decouple the hash to protect it from being modified outside the
# object
my %hash = %{$self->{Translation}};
return \%hash;
}
=item 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.
=cut
sub incremental {
my $self = shift;
if (@_) { $self->{INC} = shift; }
return $self->{INC};
}
=item 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 are 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.)
Default is true.
=cut
sub full_options {
my $self = shift;
if (@_) { $self->{FullOptions} = shift; }
return $self->{FullOptions};
}
=item 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.
=cut
sub casesens {
my $self = shift;
if (@_) { $self->{CaseSens} = shift; }
return $self->{CaseSens};
}
=item 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.
=cut
sub minmatch {
my $self = shift;
if (@_) { $self->{MinMatch} = shift; }
return $self->{MinMatch};
}
=item 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.
=cut
sub autotrans {
my $self = shift;
if (@_) { $self->{AutoTranslate} = shift; }
return $self->{AutoTranslate};
}
=item 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.
=cut
sub casesenstrans {
my $self = shift;
if (@_) { $self->{CaseSensTrans} = shift; }
return $self->{CaseSensTrans};
}
=item 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.
=cut
sub minmatchtrans {
my $self = shift;
if (@_) { $self->{MinMatchTrans} = shift; }
return $self->{MinMatchTrans};
}
=item 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.
=cut
sub warnonmissing {
my $self = shift;
if (ref $self) {
if (@_) { $self->{WarnOnMissing}=shift;}
return $self->{WarnOnMissing};
} else {
$default->{WarnOnMissing} = shift if @_;
return $default->{WarnOnMissing};
}
}
=item debug
Turn on or off debug messages. Default is off (0).
Can be used to set or return this value.
=cut
sub debug {
my $self = shift;
if (ref $self) {
if (@_) { $self->{DEBUG} = shift; }
return $self->{DEBUG};
} else {
$default->{DEBUG} = shift if @_;
return $default->{DEBUG};
}
}
=item 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.
=cut
sub options {
my $self = shift;
# If there is an argument do something clever
if (@_) {
# check that the arg is a hash
my $arg = shift;
croak("Argument is not a hash reference") unless ref($arg) eq "HASH";
# Turn the options into a real hash
my %user = %$arg;
# Now read in the base options
my $base;
if ($self->incremental) {
$base = $self->curr_full;
} else {
$base = $self->defaults;
}
# Turn into a real hash for convenience
my %base = %$base;
# Store a list of all the expanded user keys
my @list = ();
# Read in synonyms
my %syn = %{$self->synonyms};
# Now go through the keys in the user hash and compare with
# the defaults
foreach my $userkey (sort keys %user) {
# Check for matches in the default set
my @matched = $self->compare_with_list(0, $userkey, keys %base);
# If we had no matches, check the synonyms list
if ($#matched == -1) {
@matched = $self->compare_with_list(0, $userkey, keys %syn);
# If we have matched then convert the key to the actual
# value stored in the object
for (my $i =0; $i <= $#matched; $i++) {
$matched[$i] = $syn{$matched[$i]};
}
}
# At this point we have matched the userkey to a key in the
# defaults list (or if not say so)
if ($#matched == -1) {
print "Warning: $userkey is not a valid option\n" if $self->{WarnOnMissing};
} else {
if ( $#matched > 0 ) {
print "Warning: Multiple matches for option $userkey\n";
print "Warning: Could be any of the following:\n";
print join("\n",@matched) . "\n";
print "Accepting the first match ($matched[0])\n";
}
# Modify the value in %base and keep track of a separate
# array containing only the matched keys
$base{$matched[0]} = $user{$userkey};
push(@list, $matched[0]);
print "Matched: $userkey for $matched[0]\n" if $self->debug;
}
}
# Finished matching so set this as the current state of the
# object
$self->curr_keys(@list);
$self->curr_full(\%base);
# Now process the values via the provided translation
# if required. Note that the current design means that
# We have to run this after we have set the current state.
# Otherwise the translation() method would not work directly
# and we would have to provide a public version and a private one.
# Note that translate updates the current state of the object
# So we don't need to catch the return value
$self->translate if $self->autotrans;
}
# Current state should now be in current.
# Simply return it
return $self->current;
}
=item 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;
=cut
sub translate {
my $self = shift;
my %trans = %{$self->translation};
my %opt = %{$self->curr_full}; # Process all options
# Now need to go through each of the keys
# and if the corresponding key exists in the translation
# hash we need to check that a valid translation exists
foreach my $key ( keys %opt ) {
if (exists $trans{$key}) {
# Okay so a translation might exist
# Now compare keys in the hash in the hash
my %subhash = %{$trans{$key}};
my @matched =
$self->compare_with_list(1, $opt{$key}, keys %subhash);
# At this point we have matched the userkey to a key in the
# dictionary. If there is no translation dont say anything
# since it may be a 'REAL' answer (ie 1 instead of 'red')
if ($#matched > -1) {
if ( $#matched > 0 ) {
print "Warning: Multiple matches for $opt{$key} in option $key\n";
print "Warning: Could be any of the following:\n";
print join("\n",@matched) . "\n";
print "Accepting the first match ($matched[0])\n";
}
# Modify the value in the options set
print "Translation: $opt{$key} translated to $subhash{$matched[0]}\n"
if $self->debug;
$opt{$key} = $subhash{$matched[0]};
}
}
}
# Update the current state
return $self->curr_full( \%opt );
}
# Private method to compare a key with a list of keys.
# The object controls whether case-sensitivity of minimum matching
# are required
# Arguments: flag to determine whether I am matchin options or translations
# this is needed since both methods are configurable with
# regards to minimum matching and case sensitivity.
# 0 - use $self->minmatch and $self->casesens
# 1 - use $self->minmatchtrans and $self->casesenstrans
# $key: Key to be compared
# @keys: List of keys
# Returns: Array of all keys that match $key taking into account the
# object state.
#
# There must be a more compact way of doing this
sub compare_with_list {
my $self = shift;
my $flag = shift;
my $key = shift;
my @list = @_;
my @result = ();
my ($casesens, $minmatch);
if ($flag == 0) {
$casesens = $self->casesens;
$minmatch = $self->minmatch;
} else {
$casesens = $self->casesenstrans;
$minmatch = $self->minmatchtrans;
}
# Do matches
# Case Sensitive
if ($casesens) {
# Always start with the exact match before proceding to minimum
# match.
# We want to make sure that we will always match on the
# exact match even if alternatives exist (eg COL will always
# match just COL if the keys are COL and COLOUR)
# Case insensitive
@result = grep { /^$key$/ } @list;
# Proceed to minimum match if we detected nothing
# Minumum match/ Case sensitive
if ($#result == -1 && $minmatch) {
@result = grep { /^$key/ } @list;
}
} else {
# We want to make sure that we will always match on the
# exact match even if alternatives exist (eg COL will always
# match just COL if the keys are COL and COLOUR)
# First do the exact match (case insensitive)
{
local $^W = undef; # To silence warnings about uninitialised values
@result = grep { /^$key$/i } @list;
}
# If this match came up with something then we will use it
# Else we will try a minimum match (assuming flag is true)
# Minumum match/ Case insensitive
if ($#result == -1 && $minmatch) {
@result = grep { /^$key/i } @list;
}
}
return @result;
}
=back
=head1 EXAMPLE
Two examples are shown. The first uses the simplified interface and
the second uses the object-oriented interface.
=head1 Non-OO
use PDL::Options (':Func');
%options = parse( {
LINE => 1,
COLOUR => 'red',
},
{
COLOR => 'blue'
}
);
This will return a hash containg
%options = (
LINE => 1,
COLOUR => 'blue'
)
=head1 Object oriented
The following example will try to show the main points:
use PDL::Options ();
# Create new object and supply defaults
$opt = new PDL::Options( { 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',
}
);
When this code is run, $options will be the reference to a hash
containing the following:
Colour => '#00ff00',
LineStyle => 1,
LineWidth => 1
If full_options() was set to false (0), $options would be a reference
to a hash containing:
Colour => '#00ff00',
LineStyle => 1
Minimum matching and case insensitivity can be configured for both
the initial parsing and for the subsequent translating. The translation
can be turned off if not desired.
Currently synonyms are not available for the translation although this
could be added quite simply.
=head1 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.
=cut
1;
|