package RDA::Macro::Value; # $Id: Value.pm,v 1.39 2008/02/25 14:55:26 mschenke Exp $ # ARCS: $Header: /home/cvs/repository/RDA_4/src/scripting/lib/RDA/Macro/Value.pm,v 1.39 2008/02/25 14:55:26 mschenke Exp $ # # Change History # 20080225 MSC Change the copyright notice. =head1 NAME RDA::Macro::Value - Class Used for Value Macros =head1 SYNOPSIS require RDA::Macro::Value; =head1 DESCRIPTION The objects of the C class are used to interface with value-related macros. The following methods are available: =cut use strict; BEGIN { use Exporter; use File::Basename; use RDA::Value; } # Define the global public variables use vars qw($VERSION); $VERSION = sprintf("%d.%02d", q$Revision: 1.39 $ =~ /(\d+)\.(\d+)/); # Define the global private variables my %tb_fct = ( 'and' => \&_m_and, 'assign' => \&_m_assign, 'check' => \&_m_check, 'code' => \&_m_code, 'cond' => \&_m_cond, 'defined' => \&_m_defined, 'dump' => \&_m_dump, 'eval' => \&_m_eval, 'exists' => \&_m_exists, 'grep' => \&_m_grep, 'keys' => \&_m_keys, 'list' => \&_m_list, 'missing' => \&_m_missing, 'not' => \&_m_not, 'nvl' => \&_m_nvl, 'or' => \&_m_or, 'ref' => \&_m_ref, 'reverse' => \&_m_reverse, 'scalar' => \&_m_scalar, 'values' => \&_m_values, ); my %tb_grp = ( b => \&_grep_b, bv => \&_grep_bv, d => \&_grep_d, dv => \&_grep_dv, f => \&_grep_f, fv => \&_grep_fv, ); # Report the module version number sub Version { $VERSION; } =head2 S<$h = RDA::Macro::Value-Enew($agt)> The object constructor. It takes the agent object reference as an argument. C is represented by a blessed hash reference. The following special key is used: =over 16 =item B< '_agt' > Reference to the agent object =back =cut sub new { my $slf = shift; my $class = ref($slf) || $slf; my $agt = shift; # Create the macro object $slf = bless {_agt => $agt}, $class; # Register the macros $agt->register($slf, [keys(%tb_fct)]); # Return the object reference $slf; } =head2 S<$h-Erun($name,$arg,$ctx)> This method executes the macro with the specified argument list in a given context. =cut sub run { my ($slf, $nam, $arg, $ctx) = @_; return &{$tb_fct{$nam}}($slf, $ctx, $arg); } =head1 CONDITION RELATED MACROS =head2 S This macro returns 1 when all elements in the argument list are equivalent to true. Otherwise, it returns 0. It executes code values. When an argument is false, the remaining arguments are not evaluated. =cut sub _m_and { my ($slf, $ctx, $arg) = @_; foreach my $itm (@{$arg->get_items}) { return $VAL_ZERO unless $itm->eval_as_scalar; } $VAL_ONE; } =head2 S This macro returns 1 when all arguments are not undefined. Otherwise, it returns 0. It executes code values. When an argument is not defined, the remaining arguments are not evaluated. =cut sub _m_defined { my ($slf, $ctx, $arg) = @_; foreach my $itm (@{$arg->get_items}) { return $VAL_ZERO unless defined($itm->eval_as_scalar); } $VAL_ONE; } =head2 S This macro returns 1 when the argument is equivalent to false. Otherwise, it returns 0. It executes code values. =cut sub _m_not { my ($slf, $ctx, $arg) = @_; $arg->get_item(0, $VAL_ZERO)->eval_as_scalar ? $VAL_ZERO : $VAL_ONE; } =head2 S This macro returns 1 when at least one element in the argument list is equivalent to true. Otherwise, it returns 0. It executes code values. When an argument is true, the remaining arguments are not evaluated. =cut sub _m_or { my ($slf, $ctx, $arg) = @_; foreach my $itm (@{$arg->get_items}) { return $VAL_ONE if $itm->eval_as_scalar; } $VAL_ZERO; } =head1 VARIABLE VALUE RELATED MACROS =head2 S This macro assigns the value to the specified variables and returns the value. =cut sub _m_assign { my ($slf, $ctx, $arg) = @_; my $val; $arg = RDA::Value->new('L',[@{$arg->get_items}]); $val = $arg->pop_item($VAL_EMPTY); $arg->assign_value($val); } =head2 S This macro checks for the first matching regular expression. When a match is found, it evaluates the corresponding expression and it returns the result. You can specify a default return value as an extra argument. If the default value is omitted, it returns an undefined value. It only evaluates arguments when required. You can use code values to specify a list of regular expressions and the corresponding values. =cut sub _m_check { my ($slf, $ctx, $arg) = @_; my ($flg, $str, $val, @arg); $val = $VAL_UNDEF; @arg = @{$arg->get_items}; if (ref($str = shift(@arg))) { $str = $str->eval_as_string; ($flg, $val) = _check($str, \@arg); return $val if $flg; } $val; } sub _check { my ($str, $lst) = @_; my ($flg, $ref, $val); while (defined($ref = shift(@$lst))) { next unless ref($ref); $ref = $ref->eval_value; if ($ref->{'typ'} eq 'X') { ($flg, $val) = _check($str, $ref->{'val'}); return (1, $val->eval_value) if $flg; } else { return (0, $ref) unless defined($val = shift(@$lst)); $ref = $ref->eval_as_string(1); return (1, $val->eval_value) if $str =~ qr#$ref#; } } (0, $VAL_UNDEF); } =head2 S This macro constructs a code value based on the specified arguments without evaluating them. =cut sub _m_code { my ($slf, $ctx, $arg) = @_; RDA::Value->new('X', $arg->get_items); } =head2 S This macro evaluates the conditions successively until a true condition is encountered. It executes code values in condition. When a true condition is found, it evaluates the corresponding expression and it returns the result. You can specify a default return value as an extra argument. If the default value is omitted, it returns an undefined value. It only evaluates arguments when required. =cut sub _m_cond { my ($slf, $ctx, $arg) = @_; my ($cnd, $val, @arg); @arg = @{$arg->get_items}; while (($cnd, $val) = splice(@arg, 0, 2)) { return $cnd->eval_value unless defined($val); return $val->eval_value if $cnd->eval_as_scalar; } $VAL_UNDEF; } =head2 S This macro dumps the specified objects. It executes code values. =cut sub _m_dump { my ($slf, $ctx, $arg) = @_; my $val; foreach my $itm (@{$arg->get_items}) { $val = $itm->eval_value(1); $val->{'val'}->dump if ref($val) eq 'RDA::Value' && $val->{'typ'} eq 'O'; } $VAL_UNDEF; } =head2 S This macro evaluates each value from the argument list. It executes code values. It returns the last result. =cut sub _m_eval { my ($slf, $ctx, $arg) = @_; my $ret; $ret = $VAL_UNDEF; foreach my $itm (@{$arg->get_items}) { $ret = $itm->eval_value(1); } $ret; } =head2 S This macro indicates when a variable, an array index, or a hash key exists. =cut sub _m_exists { _exists(@_) ? $VAL_ONE : $VAL_ZERO; } sub _exists { my ($slf, $ctx, $arg) = @_; my ($dic, $nam, $typ, $var); $var = $arg->get_item(0); if ($var && exists($var->{'nam'})) { $dic = $var->{'ctx'}->get_dict; $nam = $var->{'nam'}; # Check if the variable exists return 0 unless exists($dic->{$nam}); # Make more specific tests $typ = $var->{'typ'}; if ($typ eq 'I') { my ($idx, $rec); $idx = int($var->{'arg'}->eval_as_number); $rec = $dic->{$nam}->{'ref'}->{'val'}; return 1 unless $idx > $#$rec || $idx < (-$#$rec - 1); } elsif ($typ eq 'K') { my ($key, $rec); $key = $var->{'arg'}->eval_as_string; $rec = $dic->{$nam}->{'ref'}; return 1 if exists($rec->{$key}); } elsif ($typ eq 'V') { return 1; } } 0; } =head2 S or grep(%var,cond) This macro evaluates the specified code value for each array value or the hash key (setting C to each element). It returns the list value consisting of those elements for which the expression evaluated as true. =head2 S or grep(%var,$re[,$opt]) This macro returns the array values or the hash keys that correspond to the specified pattern. It ignores undefined array elements. The following options are supported: =over 9 =item B< 'b' > Considers only the base name part of the value =item B< 'd' > Considers only the directory part of the value =item B< 'i' > Ignores case distinctions in both the pattern and the value =item B< 'v' > Inverts the sense of matching to select non-matching value =back =cut sub _m_grep { my ($slf, $ctx, $arg) = @_; my ($cnd, $dic, $fct, $opt, $var, @tbl); # Get the arguments $var = $arg->get_item(0); die("RDA-00516: Expecting an array or a hash variable as first argument\n") unless ref($var) && index('VR', $var->{'typ'}) >= 0 && $var->{'var'} =~ m/[\@\%]/; $dic = $var->{'ctx'}->get_dict; return $VAL_EMPTY unless exists($dic->{$var->{'nam'}}); $var = $dic->{$var->{'nam'}}->{'ref'}; $cnd = $arg->get_item(1, $VAL_NONE)->eval_value; # Treat code based filtering if ($cnd->{'typ'} eq 'X') { return RDA::Value::new_list(grep {_grep_x($ctx, RDA::Value->new('T', $_), $cnd)} keys(%$var)) if ref($var) eq 'HASH'; return RDA::Value->new('L', [grep {ref($_) && _grep_x($ctx, $_, $cnd)} @{$var->{'val'}}]); } # Decode the options $cnd = $cnd->eval_as_string(1); $opt = '' unless defined($opt = $arg->get_item(2, $VAL_NONE)->eval_as_string); $cnd = (index($opt, 'i') < 0) ? qr#$cnd# : qr#$cnd#i; $fct = (index($opt, 'b') >= 0) ? 'b' : (index($opt, 'd') >= 0) ? 'd' : 'f'; $fct .= 'v' if index($opt, 'v') >= 0; # Filter the list $fct = $tb_grp{$fct}; return RDA::Value::new_list(grep {&$fct($_, $cnd)} keys(%$var)) if ref($var) eq 'HASH'; return RDA::Value->new('L', [grep {ref($_) && $_->{'typ'} eq 'T' && &$fct($_->{'val'}, $cnd)} @{$var->{'val'}}]); } sub _grep_b { my ($val, $re) = @_; basename($val) =~ $re; } sub _grep_bv { my ($val, $re) = @_; basename($val) !~ $re; } sub _grep_d { my ($val, $re) = @_; dirname($val) =~ $re; } sub _grep_dv { my ($val, $re) = @_; dirname($val) !~ $re; } sub _grep_f { my ($val, $re) = @_; $val =~ $re; } sub _grep_fv { my ($val, $re) = @_; $val !~ $re; } sub _grep_x { my ($ctx, $val, $cnd) = @_; $ctx->{'_val'} = $val; $cnd->eval_as_scalar; } =head2 S This macro returns the list of all keys used in the specified hash. By default, it sorts the keys in alphabetical order. You can specify sort criteria as an argument: =over 10 =item B< 'NA' > By their value, sorted numerically ascending =item B< 'ND' > By their value, sorted numerically descending =item B< 'SA' > By their value, sorted alphabetically =item B< 'SD' > By their value, in reverse order =back The hash is not implicitly defined by this macro. =cut sub _m_keys { my ($slf, $ctx, $arg) = @_; my ($opt, $var, %tbl); # Get the hash $var = $arg->get_item(0); die("RDA-00500: Expecting a hash variable as argument\n") unless ref($var) && index('VR', $var->{'typ'}) >= 0 && $var->{'var'} eq '%'; $var = $var->{'ctx'}->get_dict->{$var->{'nam'}}; $var = ref($var) ? $var->{'ref'} : {}; # Get the sort type $opt = $arg->get_item(1, $VAL_NONE)->eval_as_string; # Return the keys sorted if ($opt =~ m/^N([AD])$/) { foreach my $key (keys(%$var)) { $tbl{$key} = $var->{$key}->eval_as_number; } return RDA::Value::new_list(sort {$tbl{$a} <=> $tbl{$b} || $a cmp $b} keys(%tbl)) if $1 eq 'A'; return RDA::Value::new_list(sort {$tbl{$b} <=> $tbl{$a} || $a cmp $b} keys(%tbl)) if $1 eq 'D'; } if ($opt =~ m/^S([AD])$/) { foreach my $key (keys(%$var)) { $tbl{$key} = $var->{$key}->eval_as_string; } return RDA::Value::new_list(sort {$tbl{$a} cmp $tbl{$b} || $a cmp $b} keys(%tbl)) if $1 eq 'A'; return RDA::Value::new_list(sort {$tbl{$b} cmp $tbl{$a} || $a cmp $b} keys(%tbl)) if $1 eq 'D'; } RDA::Value::new_list(sort keys(%$var)); } =head2 S This macro returns the argument list after its evaluation. It executes the code values. =cut sub _m_list { my ($slf, $ctx, $arg) = @_; $arg->eval_value(1); } =head2 S This macro indicates when a variable, an array index, or a hash key does not exist. =cut sub _m_missing { _exists(@_) ? $VAL_ZERO : $VAL_ONE; } =head2 S This macro returns the value of the first argument that is defined. It executes code values. It does not evaluate the remaining arguments. If it does not find a defined value, then it returns an undefined value. =cut sub _m_nvl { my ($slf, $ctx, $arg) = @_; my $val; foreach my $itm (@{$arg->get_items}) { return RDA::Value->new('T', $val) if defined($val = $itm->eval_as_scalar); } $VAL_UNDEF; } =head2 S This macro returns the object class if the value is a reference to an object. Otherwise, it returns an empty string. It executes code values. =cut sub _m_ref { my ($slf, $ctx, $arg) = @_; my $val; $val = $arg->get_item(0); $val = $val->eval_value(1) if ref($val); RDA::Value->new('T', (ref($val) eq 'RDA::Value' && $val->{'typ'} eq 'O') ? ref($val->{'val'}) : ''); } =head2 S This macro returns the reverse list of the arguments. It executes code values. =cut sub _m_reverse { my ($slf, $ctx, $arg) = @_; RDA::Value::new_list(reverse $arg->eval_as_array); } =head2 S This macro evaluates its argument list in a scalar context. It executes code values. =cut sub _m_scalar { my ($slf, $ctx, $arg) = @_; $arg = $arg->eval_value(1); ($arg->{'typ'} eq 'L') ? RDA::Value->new('N', scalar(@{$arg->{'val'}})) : $arg; } =head2 S This macro returns the list of all values used in the specified hash. The hash is not implicitly defined by this macro. =cut sub _m_values { my ($slf, $ctx, $arg) = @_; my $var; $var = $arg->get_item(0); die("RDA-00500: Expecting a hash variable as argument\n") unless ref($var) && index('VR', $var->{'typ'}) >= 0 && $var->{'var'} eq '%'; $var = $var->{'ctx'}->get_dict->{$var->{'nam'}}; $var = ref($var) ? $var->{'ref'} : {}; RDA::Value->new('L', [values(%$var)]); } 1; __END__ =head1 SEE ALSO See also L, L, L, L. =head1 COPYRIGHT NOTICE Copyright (c) 2002-2008, Oracle. All rights reserved. =head1 TRADEMARK NOTICE Oracle, JD Edwards, PeopleSoft, and Siebel are registered trademarks of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. =cut