Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Dear monks,

I wrote a little script called (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 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."

In reply to returning tied array by strat

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2021-06-22 02:16 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (100 votes). Check out past polls.