xiaoyafeng has asked for the wisdom of the Perl Monks concerning the following question:
Hi monks
Recently, I've been writing a excel template program based on Win32::OLE. for reusing and code elegance, I've used a callback like what File::Find does. Below is sketch of framework:
below are some part of codes:----------- ------------ ------------- |excel | | | | | |template | <== | callback | <==== |database | | | | | |___________| | | | | --------- |___________| <===== ------------- | | |DCOM server | | | --------------
Please note $hl in above code, it is a ref from another big object including DBI and Dcom server handles. at this stage, I new it as a global variable and use it in subroutine directly. it make work done but looks ugly.sub cell_walk{ my $self = shift; my ($sheet_name, $x, $y, $callback) = @_; my $Sheet = $self->{ 'book_handle' }->Worksheets($sheet_name); for ( $row = $x->[0] ; $row <= $y->[0] ; $row++ ) { for ( my $col = $x->[1] ; $col <= $y->[1] ; $col++ ) { $callback->($Sheet->Cells( $row, $col )); } } } sub callback{ return if !defined $_[0]->{Value}; if ( $_[0]->{Value} =~ /^~~~/ ) { my @a = split( /__/, substr( $_[0]->{Value}, 3 ) ); #grab usefu +l string if( scalar @a == 3){ $_[0]->{Value} = $hl->get_single_LP( 'ADAS_VAL_RAW', @a ); } if( scalar @a == 4){ $_[0]->{Value} = $hl->accu_LP( 'ADAS_VAL_NORM', @a ); } } }
So Is there any good way to re-organize code to make my source more pretty? Do I need to pass the ref of the obj to callback in advance? Thanks in advance!!!!
UPDATE: correct codes.
I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: how organize code
by Khen1950fx (Canon) on Nov 29, 2011 at 07:20 UTC | |
|
Re: how organize code in callback mode
by roboticus (Chancellor) on Nov 29, 2011 at 11:41 UTC | |
by xiaoyafeng (Deacon) on Nov 29, 2011 at 12:38 UTC | |
by patcat88 (Deacon) on Nov 29, 2011 at 18:35 UTC | |
|
Re: how organize code
by patcat88 (Deacon) on Nov 29, 2011 at 08:01 UTC |