sub calc { ## We operate in package main for this subroutine so the local ## object and row number can be available to the caller's $Sub. ## The following local vars will be available to $Sub ## $_r ($main::_r) -- the row number in the entire table ## $_s ($main::_s) -- the row number in selection or $Recs ## $_t ($main::_t) -- the table object itself package main; use vars qw($_r $_s $_t); local ($_r, $_s, $_t); $_t = shift; my ($Sub, $Recs, $Fields) = @_; ## These optional params default to current field and current sel $Recs ||= $_t->selection(); $Fields ||= $_t->fieldlist_all(); ## Local copy of symbol table. Didn't seem to help. Odd. ## local %main::; ## We'll build a column of return values from $Sub if needed. my $WantVals = defined(wantarray); my $Vals = $_t->col_empty() if $WantVals; ## Call col() on each field in list to make sure it exists. foreach (@$Fields) {$_t->col($_)}; foreach $_s (0..$#$Recs) { $_r = $Recs->[$_s]; ## Bind $FieldName1, $FieldName2, (etc. for each field name in ## $Fields) point to address of the current value for that ## field in this record. no strict 'refs'; foreach my $F (@$Fields) {*{$F} = \ $_t->{$F}->[$_r]}; ## Now $Sub may refer to $_r, $_s, $_t, and ${any field name} ## Call $Sub and capture return values iff caller wants them ($WantVals ? $Vals->[$_r] = &$Sub() : &$Sub()); } ## Return scalar column ref unless return context is undef return($WantVals ? $Vals : ()); }