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"


In reply to returning tied array by strat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.