http://www.perlmonks.org?node_id=593388

strat has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks,

I wrote a little script called excelPerl.pl (like xlsPerl, but using Win32::OLE for getting read/write-access to the excel file) which tries to do to an excel file what perl -ane does for a text file, and to keep the interface as similar as possible, I also use the array @F for accessing the cells of one row. Since I want to save changes of elements of @F into the excel file, I decided to use a tied array.

The program works fine, but I couldn't find a clean way to return the tied array from a subroutine so I can use it as array (and not as array reference)

Here a minimal code example returning as arrayref which works:

my $array = TieVariable(); ### Pos1 print "### Initialization finished\n"; for (0..$#$array) { print "$_: $array->[$_]\n"; } # for $array->[6] = 1000; untie $array; # ------------------------------------------------------------ sub TieVariable { tie my @array, 'Tie::Excel::Array'; $array[$_] = 5 * $_ for 0..5; return \@array; ### Pos2 } # TieVariable # ============================================================ package Tie::Excel::Array; sub TIEARRAY { my( $class ) = @_; return bless [], $class; } # TIEARRAY # ------------------------------------------------------------ sub STORE { my( $self, $index, $value ) = @_; warn "STORE: \$self->[$index] = $value;\n"; $self->[$index] = $value; } # STORE # ------------------------------------------------------------ sub FETCH { my( $self, $index ) = @_; warn "FETCH: \$self->[$index] = $self->[$index];\n"; return $self->[$index]; } # STORE # ------------------------------------------------------------ sub FETCHSIZE { my( $self ) = @_; return $#$self; } # FETCHSIZE # ------------------------------------------------------------ sub UNTIE { my( $self ) = @_; warn "UNTIE\n"; } # UNTIE # ...

Using array

If I just change POS1 to my @array = TieVariable(); and POS2 to return @array; to be able to work with an array in my main program, the array looses its binding, and the change in $array->[6] = 1000; can't be written to the excel file.

Using a global variable

If I use @F as global variable and fill it, it would work; but then I have to know the name of the global variable in the subroutine, and can only use one @F.

Working with an alias

using a package variable and working with something like: our @F; *F = \@MyPackage::Array1;, but then I have to know the variable's name in my main program, and can only use one @MyPackage::Array1

Move the tie into the main program

I like this solution best, but then in the main program you have to know you are working with a tied array

Writing a sourcefilter or similar

Working with the arrayref-solution and writing a source filter which converts accesses to $F[0] to $F->[0] looks much to complicated.

Do you know another solution how I can do it?

The main program of excelPerl.pl should look something like:

# ... parsing parameters: $filename, $perlCode, $separator, ... my $xls = XlsPerl->new( ... parameters ... ); while( my @F = $xls->getNextRowData ) { ### POS 1 local $_ = join( $separator, @F ); eval $perlCode; die $@ if $@; } # while

Best regards,
perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"