package RDA::Value; # $Id: Value.pm,v 1.41 2009/01/16 18:23:57 ldens Exp $ # ARCS: $Header: /u02/webapps/workbench/cvs/RDA_4/src/scripting/lib/RDA/Value.pm,v 1.41 2009/01/16 18:23:57 ldens Exp $ # # Change History # 20090116 LDE Improve documentation. =head1 NAME RDA::Value - Class for Objects to Manage Values =head1 SYNOPSIS require RDA::Value; =head1 DESCRIPTION The objects of the C class are used to manage values. The following value types are supported: =over 9 =item B< 'C' > Last context value =item B< 'I' > Value associated with an array index =item B< 'K' > Value associated with a hash key =item B< 'L' > List =item B< 'M' > Macro =item B< 'N' > Number =item B< 'O' > Object =item B< 'P' > Variable pointer =item S< 'R' > Variable reference =item B< 'T' > Text string =item B< 'U' > Undef =item B< 'V' > Variable =item B< 'X' > Code to execute =back The following methods are available: =cut use strict; BEGIN { use Exporter; use vars qw($VERSION @ISA @EXPORT $VAL_EMPTY $VAL_NONE $VAL_ONE $VAL_UNDEF $VAL_ZERO); @ISA = qw(Exporter); @EXPORT = qw($VAL_EMPTY $VAL_NONE $VAL_ONE $VAL_UNDEF $VAL_ZERO); } # Define the global public variables $VERSION = sprintf("%d.%02d", q$Revision: 1.41 $ =~ /(\d+)\.(\d+)/); # Define the global private constants my $OBJECT = qr/^RDA::Object::/; my %tb_init = ( A => \&_init_arg, C => \&_init_ctx, I => \&_init_elem, K => \&_init_elem, L => \&_init_list, M => \&_init_macro, N => \&_init_num, O => \&_init_obj, P => \&_init_ptr, R => \&_init_var, T => \&_init_text, U => \&_init_undef, V => \&_init_var, X => \&_init_list, ); # Define global special values $VAL_EMPTY = RDA::Value->new('L'); $VAL_NONE = RDA::Value->new('T'); $VAL_ONE = RDA::Value->new('N', 1); $VAL_UNDEF = RDA::Value->new('U'); $VAL_ZERO = RDA::Value->new('N', 0); =head2 S<$h = RDA::Value-Enew($typ,...)> The object constructor. The list of arguments depends on the specified value type: =over 9 =item B< 'A' > $ref =item B< 'C' > $ctx =item B< 'I' > $ctx,$nam,$arg =item B< 'K' > $ctx,$nam,$arg =item B< 'L' > $val (empty list by default) =item B< 'M' > $ctx,$nam,$arg (empty argument list by default) =item B< 'N' > $val (0 by default) =item B< '0' > $val =item B< 'P' > $var,$ref (storage reference created by default) =item B< 'R' > $ctx,$nam =item B< 'T' > $val (empty string by default) =item B< 'U' > (no argument) =item B< 'V' > $ctx,$nam,$dft (undef or empty list by default) =item B< 'X' > $val (empty list by default) =back C is represented by a blessed hash reference. The following special keys are used: =over 12 =item S< B<'arg' > > Reference to an argument list =item S< B<'ctx' > > Reference to the execution context =item S< B<'nam' > > Macro name =item S< B<'ref' > > Associated reference =item S< B<'typ' > > Value type =item S< B<'val' > > Associated value =item S< B<'var' > > Variable type =back =cut sub new { my $slf = shift; my $cls = ref($slf) || $slf; my $typ = shift || 'U'; # Validate the value type die "RDA-00250: Invalid value type '$typ'\n" unless exists($tb_init{$typ}); # Create the data collection object $slf = bless {typ => $typ}, $cls; # Make specific initialisation &{$tb_init{$typ}}($slf, @_); # Return the object reference $slf; } # Initialise the object based on its type sub _init_arg { my ($slf, $ref) = @_; return _init_undef($slf) unless ref($ref); $slf->{'typ'} = 'P'; $slf->{'var'} = '@'; $slf->{'ref'} = $ref; } sub _init_ctx { my ($slf, $ctx) = @_; return _init_undef($slf) unless ref($ctx); $slf->{'ctx'} = $ctx; } sub _init_elem { my ($slf, $ctx, $nam, $arg) = @_; return _init_undef($slf) unless ref($ctx) && defined($nam) && ref($arg); $slf->{'ctx'} = $ctx; $slf->{'nam'} = $nam; $slf->{'var'} = '$'; $slf->{'arg'} = $arg; } sub _init_list { my ($slf, $val) = @_; $slf->{'val'} = ref($val) ? $val : []; } sub _init_macro { my ($slf, $ctx, $nam, $arg) = @_; return _init_undef($slf) unless ref($ctx) && defined($nam); $slf->{'ctx'} = $ctx; $slf->{'nam'} = $nam; $slf->{'arg'} = ref($arg) ? $arg : $VAL_EMPTY; } sub _init_num { my ($slf, $val) = @_; $slf->{'val'} = defined($val) ? $val : 0; } sub _init_obj { my ($slf, $val) = @_; return _init_undef($slf) unless ref($val) =~ $OBJECT; $slf->{'val'} = $val; } sub _init_ptr { my ($slf, $var, $ref) = @_; return _init_undef($slf) unless ref($var) && index('VIKR', $var->{'typ'}) >= 0; $slf->{'var'} = substr($var->{'nam'}, 0, 1); $slf->{'ref'} = ref($ref) ? $ref : exists($var->{'dft'}) ? $var->{'dft'} : ($slf->{'var'} eq '%') ? {} : ($slf->{'var'} eq '@') ? RDA::Value->new('L') : $VAL_UNDEF; } sub _init_text { my ($slf, $val) = @_; $slf->{'val'} = defined($val) ? $val : ''; } sub _init_undef { my ($slf) = @_; $slf->{'typ'} = 'U'; $slf->{'val'} = undef; } sub _init_var { my ($slf, $ctx, $nam, $dft) = @_; return _init_undef($slf) unless ref($ctx) && defined($nam); $slf->{'ctx'} = $ctx; $slf->{'var'} = substr($nam, 0, 1); $slf->{'nam'} = $nam; $slf->{'dft'} = $dft if ref($dft); } =head2 S<$h = RDA::Value::new_list($arg,...)> This method creates a list where each argument is converted into text, object, or undefined values. =cut sub new_list { RDA::Value->new('L', [map {RDA::Value->new(ref($_) ? 'O' : defined($_) ? 'T' : 'U', $_)} @_]); } =head2 S<$h-Edump> This method dumps a value. =cut sub dump { my $slf = shift; my $lvl = shift || 0; my $str = shift || ''; my $buf; $buf = ' ' x $lvl++; $buf .= $str.$slf->{'typ'}; $buf .= " type=".$slf->{'var'} if exists($slf->{'var'}); $buf .= " name=".$slf->{'nam'} if exists($slf->{'nam'}); if (exists($slf->{'arg'})) { print "$buf args:\n"; $slf->{'arg'}->dump($lvl); } elsif (exists($slf->{'val'})) { my $rec = $slf->{'val'}; if (ref($rec) eq 'RDA::Value') { print "$buf value:\n"; $rec->dump($lvl); } elsif (ref($rec) =~ $OBJECT) { print "$buf value:\n"; $rec->dump($lvl); } elsif (ref($rec) eq 'ARRAY') { print "$buf value:\n"; foreach my $itm (@$rec) { if (defined($itm)) { $itm->dump($lvl); } else { $buf = ' ' x $lvl; print "$buf\n"; } } } elsif (defined($rec)) { print "$buf value=$rec\n"; } else { print "$buf\n"; } } elsif (exists($slf->{'ref'})) { my $rec = $slf->{'ref'}; print "$buf storage:\n"; if (ref($rec) eq 'RDA::Value') { $rec->dump($lvl); } elsif (ref($rec) eq 'HASH') { foreach my $key (sort keys(%$rec)) { $rec->{$key}->dump($lvl, "'$key' => "); } } } else { print "$buf\n"; } } =head2 S<$h-Eget_info($key[,$dft])> This method returns the value that is associated with a given key. When the key does not exist, it returns the default value. =cut sub get_info { my ($slf, $key, $val) = @_; $val = $slf->{$key} if exists($slf->{$key}); $val; } =head2 S<$h-Eis_code> This method indicates whether the value is a code value. =cut sub is_code { shift->{'typ'} eq 'X'; } =head2 S<$h-Eis_list> This method indicates whether the value is a list. =cut sub is_list { shift->{'typ'} eq 'L'; } =head2 S<$h-Eis_lvalue> This method indicates whether the value can be used as left value(s). =cut sub is_lvalue { my ($slf) = @_; if ($slf->{'typ'} eq 'L') { foreach my $rec (@{$slf->{'val'}}) { return 0 if index('VIKRU', $rec->{'typ'}) < 0; } } else { return 0 if index('VIKRU', $slf->{'typ'}) < 0; } 1; } =head2 S<$h-Eis_macro> This method indicates whether the value is a macro call. =cut sub is_macro { shift->{'typ'} eq 'M'; } =head2 S<$h-Eis_pointer> This method indicates whether the value is a variable pointer. =cut sub is_pointer { shift->{'typ'} eq 'P'; } =head2 S<$h-Eis_ref_value> This method indicates whether the value is a reference. =cut sub is_ref_value { shift->{'typ'} eq 'R'; } =head2 S<$h-Eis_scalar_lvalue> This method indicates whether the value is a left value that requires a scalar in assignment. =cut sub is_scalar_lvalue { my ($slf) = @_; index('VIK', $slf->{'typ'}) >= 0 && $slf->{'var'} eq '$'; } =head2 S<$h-Eis_undef> This method indicates whether the value is undefined. =cut sub is_undef { shift->{'typ'} eq 'U'; } =head2 S<$h-Eis_var> This method indicates the variable type, or an empty string when the value is not a variable name. =cut sub is_var { my ($slf) = @_; ($slf->{'typ'} eq 'V') ? $slf->{'var'} : ''; } =head2 S<$h-Eis_var_list([$flg])> This method indicates whether the value is a list of variable names. When the flag is set, it accepts array and hash elements. =cut sub is_var_list { my ($slf, $flg) = @_; my $typ = $flg ? 'VIK' : 'V'; return 0 unless $slf->{'typ'} eq 'L'; foreach my $itm (@{$slf->{'val'}}) { return 0 if index($typ, $itm->{'typ'}) < 0; } 1; } =head2 S<$h-Eset_info($key,$val)> This method assigns the specified value to a given key. =cut sub set_info { my ($slf, $key, $val) = @_; $slf->{$key} = $val; } =head1 ASSIGN AND EVAL METHODS =head2 S<$h-Eassign_value($val[,$flg])> This method assigns a new value to the current value. It evaluates the new value unless the flag is set. It returns the new value. =cut sub assign_value { my ($slf, $val, $flg) = @_; my ($rec, $typ, $var, @tlv, @trv); # Evaluate the value $val = $val->eval_value unless $flg; # Perform the assignment $typ = $slf->{'typ'}; if ($slf->{'typ'} eq 'L') { # Treat a list as left value $val = RDA::Value->new('L', [$val]) unless $val->{'typ'} eq 'L'; push(@tlv, @{$slf->{'val'}}); push(@trv, @{$val->{'val'}}); while (defined($var = shift(@tlv))) { $typ = $var->{'typ'}; if ($typ eq 'R') { $var->_assign_ref(shift(@trv)); } elsif ($typ eq 'I') { $var->_assign_index(shift(@trv)); } elsif ($typ eq 'K') { $var->_assign_key(shift(@trv)); } elsif ($typ eq 'V') { $typ = $var->{'var'}; $var->_assign_var( ($typ eq '@') ? RDA::Value->new('L', [splice(@trv, 0)]) : ($typ eq '%') ? _gen_hash(\@trv) : shift(@trv) || $VAL_UNDEF ); } elsif ($typ eq 'U') { shift(@trv); } } } elsif ($typ eq 'R') { $slf->_assign_ref($val); } elsif ($typ eq 'I') { $slf->_assign_index( ($val->{'typ'} ne 'L') ? $val : RDA::Value->new('N', scalar(@{$val->{'val'}})) ); } elsif ($typ eq 'K') { $slf->_assign_key( ($val->{'typ'} ne 'L') ? $val : RDA::Value->new('N', scalar(@{$val->{'val'}})) ); } elsif ($typ eq 'V') { $typ = $slf->{'var'}; if ($typ eq '@') { $slf->_assign_var( ($val->{'typ'} eq 'L') ? $val : RDA::Value->new('L', [$val]) ); } elsif ($typ eq '%') { $val = RDA::Value->new('L', [$val]) unless $val->{'typ'} eq 'L'; push(@trv, @{$val->{'val'}}); $slf->_assign_var(_gen_hash(\@trv)); } else { $slf->_assign_var( ($val->{'typ'} ne 'L') ? $val : RDA::Value->new('N', scalar(@{$val->{'val'}})) ); } } # Return the value $val; } sub _assign_index { my ($slf, $val) = @_; my ($dic, $idx, $nam); $dic = $slf->{'ctx'}->get_dict; $nam = $slf->{'nam'}; $dic->{$nam} = RDA::Value->new('P', $slf) unless exists($dic->{$nam}); $idx = int($slf->{'arg'}->eval_as_number); $val = $VAL_UNDEF unless ref($val); $dic->{$nam}->{'ref'}->{'val'}->[$idx] = $val; $dic->{$nam}->dump(1, $slf->{'ctx'}->get_prefix("$nam: ")) if $slf->{'ctx'}->chk_trace(2); } sub _assign_key { my ($slf, $val) = @_; my ($dic, $key, $nam); $dic = $slf->{'ctx'}->get_dict; $nam = $slf->{'nam'}; $dic->{$nam} = RDA::Value->new('P', $slf, {}) unless exists($dic->{$nam}); $key = $slf->{'arg'}->eval_as_string; $val = $VAL_UNDEF unless ref($val); $dic->{$nam}->{'ref'}->{$key} = $val; $dic->{$nam}->dump(1, $slf->{'ctx'}->get_prefix("$nam: ")) if $slf->{'ctx'}->chk_trace(2); } sub _assign_ref { my ($slf, $val) = @_; my ($dic, $nam); die "RDA-00251: Invalid reference assignment\n" unless ref($val) && $val->{'typ'} eq 'P'; die "RDA-00252: Incompatible types\n" unless $slf->{'var'} eq $val->{'var'}; $dic = $slf->{'ctx'}->get_dict; $nam = $slf->{'nam'}; $dic->{$nam} = $val; $val->dump(1, $slf->{'ctx'}->get_prefix("$nam: ")) if $slf->{'ctx'}->chk_trace(2); } sub _assign_var { my ($slf, $val) = @_; my ($dic, $nam); $dic = $slf->{'ctx'}->get_dict; $nam = $slf->{'nam'}; if (exists($dic->{$nam})) { $dic->{$nam}->{'ref'} = $val; } else { $dic->{$nam} = RDA::Value->new('P', $slf, $val); } $dic->{$nam}->dump(1, $slf->{'ctx'}->get_prefix("$nam: ")) if $slf->{'ctx'}->chk_trace(2); } sub _gen_hash { my ($tbl) = @_; my ($key, $ptr, $val); $ptr = {}; while (($key, $val) = splice(@$tbl, 0, 2)) { $ptr->{$key->eval_as_string(1)} = defined($val) ? $val : $VAL_UNDEF; } $ptr; } =head2 S<$h-Edelete_value> This method deletes a left value or a list of left values. =cut sub delete_value { my ($slf) = @_; my ($dic, $nam, $typ); $typ = $slf->{'typ'}; if ($typ eq 'L') { foreach my $itm (@{$slf->{'val'}}) { $itm->delete_value if ref($itm); } } elsif ($typ eq 'I') { $dic = $slf->{'ctx'}->get_dict; $nam = $slf->{'nam'}; if (exists($dic->{$nam})) { my $idx = int($slf->{'arg'}->eval_as_number); my $rec = $dic->{$nam}->{'ref'}->{'val'}; $rec->[$idx] = $VAL_UNDEF unless $idx > $#$rec; $dic->{$nam}->dump(1, $slf->{'ctx'}->get_prefix("$nam: ")) if $slf->{'ctx'}->chk_trace(2); } } elsif ($typ eq 'K') { $dic = $slf->{'ctx'}->get_dict; $nam = $slf->{'nam'}; if (exists($dic->{$nam})) { my $key = $slf->{'arg'}->eval_as_string; delete($dic->{$nam}->{'ref'}->{$key}); $dic->{$nam}->dump(1, $slf->{'ctx'}->get_prefix("$nam: ")) if $slf->{'ctx'}->chk_trace(2); } } elsif ($typ eq 'V') { $dic = $slf->{'ctx'}->get_dict; $nam = $slf->{'nam'}; delete($dic->{$nam}); print ' '.$slf->{'ctx'}->get_prefix("$nam deleted\n") if $slf->{'ctx'}->chk_trace(2); } } =head2 S<$h-Eeval_code($dft)> This method evaluates a code value. It returns the value of the last code argument or the default value when the list is empty. =cut sub eval_code { my ($slf, $val) = @_; return $slf unless $slf->{'typ'} eq 'X'; foreach my $itm (@{$slf->{'val'}}) { $val = $itm->eval_value(1); } $val; } =head2 S<$h-Eeval_macro($alt)> This method evaluates a macro and returns its value. An extra parameter can be specified to alter macro definitions temporarily. =cut sub eval_macro { my ($slf, $alt) = @_; my ($ctx, $lib, $nam, $val); return $slf unless $slf->{'typ'} eq 'M'; $ctx = $slf->{'ctx'}; $lib = $ctx->get_lib; $nam = $slf->{'nam'}; if ($alt && exists($alt->{$nam})) { $val = $alt->{$nam}->run($nam, $slf->{'arg'}, $ctx); } elsif (exists($lib->{$nam})) { $val = $lib->{$nam}->run($nam, $slf->{'arg'}, $ctx); } else { die "RDA-00254: Undefined macro '$nam'\n"; } die "RDA-00253: Error encountered in macro '$nam' called\n" unless ref($val); $val; } =head2 S<$h-Eeval_value([$flg])> This method evaluates a value. It resolves the variables and executes the appropriate macro calls. If there is an evaluation problem, it returns an undefined value. When the flag is set, it executes code values. =cut sub eval_value { my ($slf, $flg) = @_; my ($rec, $typ); # Evaluate the value $typ = $slf->{'typ'}; $rec = $slf; if ($typ eq 'C') { $rec = $slf->{'ctx'}->{'_val'}; } elsif ($typ eq 'I') { my ($dic, $idx, $nam); $dic = $slf->{'ctx'}->get_dict; $nam = $slf->{'nam'}; if (exists($dic->{$nam})) { $idx = int($slf->{'arg'}->eval_as_number); $rec = $dic->{$nam}->{'ref'}->{'val'}; $rec = ($idx > $#$rec) ? $VAL_UNDEF : $rec->[$idx]; $rec = $VAL_UNDEF unless ref($rec); } else { $dic->{$nam} = RDA::Value->new('P', $slf); $rec = $VAL_UNDEF; } } elsif ($typ eq 'K') { my ($dic, $key, $nam); $dic = $slf->{'ctx'}->get_dict; $nam = $slf->{'nam'}; if (exists($dic->{$nam})) { $key = $slf->{'arg'}->eval_as_string; $rec = $dic->{$nam}->{'ref'}; $rec = exists($rec->{$key}) ? $rec->{$key} : $VAL_UNDEF; } else { $dic->{$nam} = RDA::Value->new('P', $slf, {}); $rec = $VAL_UNDEF; } } elsif ($typ eq 'L') { my ($val, @tbl); foreach my $itm (@{$rec->{'val'}}) { $val = defined($itm) ? $itm->eval_value($flg) : $VAL_UNDEF; if (index("NOTUPX", $val->{'typ'}) >= 0) { push(@tbl, $val); } elsif ($val->{'typ'} eq 'L') { foreach my $elm (@{$val->{'val'}}) { push(@tbl, defined($elm) ? $elm : $VAL_UNDEF); } } } $rec = RDA::Value->new('L', [@tbl]); } elsif ($typ eq 'M') { my ($ctx, $lib, $nam); $ctx = $slf->{'ctx'}; $lib = $ctx->get_lib; die "RDA-00254: Undefined macro '$nam'\n" unless exists($lib->{$nam = $slf->{'nam'}}); # Execute the macro but do not yet evaluate its arguments $rec = $lib->{$nam}->run($nam, $slf->{'arg'}, $ctx); die "RDA-00253: Error encountered in macro '$nam' called\n" unless ref($rec); } elsif ($typ eq 'P') { $rec = $slf->{'ref'}; $rec = _conv_hash($rec) if $slf->{'var'} eq '%'; } elsif ($typ eq 'R') { my ($dic, $nam); $dic = $slf->{'ctx'}->get_dict; $nam = $slf->{'nam'}; unless (exists($dic->{$nam})) { $dic->{$nam} = RDA::Value->new('P', $slf); print ' '.$slf->{'ctx'}->get_prefix("undefined reference $nam\n") if $slf->{'ctx'}->chk_trace(2); } $rec = $dic->{$nam}; } elsif ($typ eq 'V') { my ($dic, $nam, @tbl); # Get the variable, creating it when not yet existing $dic = $slf->{'ctx'}->get_dict; $nam = $slf->{'nam'}; unless (exists($dic->{$nam})) { $dic->{$nam} = RDA::Value->new('P', $slf); print ' '.$slf->{'ctx'}->get_prefix("undefined variable $nam\n") if $slf->{'ctx'}->chk_trace(2); } $rec = $dic->{$nam}->{'ref'}; if ($slf->{'var'} eq '@') { $rec = RDA::Value->new('L', [@{$rec->{'val'}}]) } elsif ($slf->{'var'} eq '%') { $rec = _conv_hash($rec) } } # Return the evaluation result as a value $rec = $rec->eval_code($VAL_UNDEF) if $flg; $rec; } sub _conv_hash { my ($rec) = @_; my @tbl; # Convert a hash in a list, with keys sorted alphabeticly foreach my $key (sort keys(%$rec)) { push(@tbl, RDA::Value->new('T', $key), $rec->{$key}); } RDA::Value->new('L', [@tbl]); } =head2 S<$h-Eeval_as_array([$flg])> This method evaluates the value and returns the evaluation result as a Perl list. It executes code values. When the flag is set, it converts the value without evaluating the value again. =cut sub eval_as_array { my ($slf, $flg) = @_; my $typ; # Evaluate the value $slf = $slf->eval_value(1) unless $flg; # Evaluate the value in an array context $typ = $slf->{'typ'}; return () if $typ eq 'U'; return ($slf->{'val'}) unless $typ eq 'L'; my @tbl; foreach my $itm (@{$slf->{'val'}}) { push(@tbl, $itm->{'val'}) if exists($itm->{'val'}); } @tbl; } =head2 S<$h-Eeval_as_line([$flg])> This method evaluates the value and returns the evaluation result as a text line. It executes code values. It ignores all undefined values and object references. When the flag is set, it converts the value without evaluating the value again. =cut sub eval_as_line { my ($slf, $flg) = @_; join('', grep {defined($_) && !ref($_)} $slf->eval_as_array($flg))."\n"; } =head2 S<$h-Eeval_as_number([$flg])> This method evaluates the value and returns the evaluation result as a Perl number. It executes code values. When the flag is set, it converts the value without evaluating the value again. =cut sub eval_as_number { my ($slf, $flg) = @_; my $typ; # Evaluate the value $slf = $slf->eval_value(1) unless $flg; # Evaluate the value in a numeric context $typ = $slf->{'typ'}; return $slf->{'val'} if $typ eq 'N'; return 0 if $typ eq 'U'; return scalar(@{$slf->{'val'}}) if $typ eq 'L'; return 0 + $slf->{'val'} if $typ eq 'T' && $slf->{'val'} =~ m/^[+-]?\d+(\.\d*)?([eE][\+\-]?\d+)?$/; die "RDA-00255: Not a numeric value\n"; } =head2 S<$h-Eeval_as_scalar($flg)> This method evaluates the value and returns the evaluation result as a Perl scalar. It executes code values. When the flag is set, it converts the value without evaluating the value again. =cut sub eval_as_scalar { my ($slf, $flg) = @_; # Evaluate the value $slf = $slf->eval_value(1) unless $flg; # Evaluate the value in a scalar context return ($slf->{'typ'} eq 'L') ? scalar(@{$slf->{'val'}}) : $slf->{'val'}; } =head2 S<$h-Eeval_as_string([$flg])> This method evaluates the value and returns the evaluation result as a Perl string. It returns an empty string for invalid values (such as not a text string or a number). It executes code values. When the flag is set, it converts the value without evaluating the value again. =cut sub eval_as_string { my ($slf, $flg) = @_; my $typ; # Evaluate the value $slf = $slf->eval_value(1) unless $flg; # Evaluate the value in a numeric context $typ = $slf->{'typ'}; return $slf->{'val'} if $typ eq 'T'; return ''.$slf->{'val'} if $typ eq 'N'; return $slf->{'val'}->as_string if $typ eq 'O'; return '' if $typ eq 'U'; die "RDA-00256: Not a text string\n"; } =head1 LIST MANAGEMENT METHODS =head2 S<$h-Ecount_items> This method returns the number of elements in a list. It returns -1 if the value is not a list. =cut sub count_items { my ($slf) = @_; ($slf->{'typ'} eq 'L') ? scalar(@{$slf->{'val'}}) : -1; } =head2 S<$h-Eget_item($idx[,$dft])> This method returns one item from the list. It returns the default value when the index is out of range. =cut sub get_item { my ($slf, $idx, $dft) = @_; my $rec; if ($slf->{'typ'} eq 'L') { $rec = $slf->{'val'}; return $rec->[$idx] unless $idx > $#$rec; } $dft; } =head2 S<$h-Eget_items> This method returns a reference to the list content. It points to an empty list if the value is not a list. =cut sub get_items { my ($slf) = @_; ($slf->{'typ'} eq 'L') ? $slf->{'val'} : []; } =head2 S<$h-Epop_item([$dft])> This method removes the last element from a list and returns it. It returns the default value if the list is empty or if the value is not a list. =cut sub pop_item { my ($slf, $dft) = @_; my $val; $val = pop(@{$slf->{'val'}}) if $slf->{'typ'} eq 'L'; $val = $dft unless defined($val); $val; } 1; =head2 S<$h-Epush_item($val,...)> This method adds values at the end the list. When applicable, it merges lists. =cut sub push_item { my $slf = shift; if ($slf->{'typ'} eq 'L') { foreach my $val (@_) { next unless ref($val); if ($val->{'typ'} eq 'L') { push(@{$slf->{'val'}}, @{$val->{'val'}}); } else { push(@{$slf->{'val'}}, $val); } } } $slf; } =head2 S<$h-Eshift_item([$dft])> This method removes the first element from a list and returns it. It returns the default value if the list is empty or if the value is not a list. =cut sub shift_item { my ($slf, $dft) = @_; my $val; $val = shift(@{$slf->{'val'}}) if $slf->{'typ'} eq 'L'; $val = $dft unless defined($val); $val; } 1; __END__ =head1 SEE ALSO L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L =head1 COPYRIGHT NOTICE Copyright (c) 2002-2009, 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