Title: DataPresenter: The Saga Continues
1DataPresenter The Saga Continues
- Part 1 Using Callback Functions to Filter
DataPresenter Objects - Glenn Maciag
- Glenn_Maciag_at_Kaplan.com
2Advanced Filtering the Easy Way
3Jim Keenans DataPresenter Module
- Allows users to manipulate database reports.
- Sorting
- Filtering
- Writing to files
- Lets look at a sample database report.
4Jim Keenans DataPresenter Module
- CLIENTS - JULY 26, 2001 - C O N F I D E N T I A L
PAGE 1 - SHRED WHEN NEW LIST IS RECEIVED!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!! - NAME C. NO UNIT WARD
ADMIT BIRTH - HERNANDEZ SHARON 456791 SAMSON 0217
2001-07-25 1963-08-01 - JONES TIMOTHY 803092 LAVER 0103
2001-03-19 1969-06-29 - SMITH HAROLD 359962 TRE 0111
2001-07-19 1973-10-02 - SMITH BETTY SUE 698389 SAMSON 0211
1992-01-23 1949-08-12 - THOMPSON GEORGE 786792 LAVER 0104
2001-07-26 1973-08-17 - THOMPSON JEFFREY 906786 TRE 0111
2001-07-15 1953-02-28 - VASQUEZ JORGE 456787 LAVER 0105
1986-01-17 1956-01-13 - VASQUEZ JOAQUIN 456789 SAMSON 0209
1990-11-14 1970-03-25 - WATSON JUNE 456788 LAVER 0107
1990-08-23 1970-15-23 - WELKMAN THOMAS 456790 LAVER 0110
1980-11-14 1960-14-02 - WILSON SYLVESTER 498703 LAVER 0110
1983-04-02 1953-06-22
5Jim KeenansDataPresenter Module
- Every type of object has a configuration file.
Here is the beginning of one for the
DataPresenterCensus class. - _at_fields qw(
- lastname
- firstname
- cno
- unit
- ward
- dateadmission
- datebirth)
6Filtering a DataPresenter Object
- columndateofbirth
- relationbefore
- _at_choices(01/01/1970)
- dpobject-gtselect_rows( column,relation,\_at_choice
s)
7Filtering a DataPresenter Object
- Advantages
- Elegant, natural language interface
- Easy for user to work with
- Disadvantage
- What if a user wants to enter a regular
expression?
8Advanced Filtering My First Thought
- User can pass in to the new method a compiled
regex along with its target - dpobject-gtselect_rows_adv( lastname,qr/SMITH/,
- firstname,qr/JOHN/)
- Within the method, I could then use an eval to
filter the data accordingly. - But I decided I wanted something more general.
9Filtering using Callback Functions
- A callback function is an ordinary subroutine
whose reference is passed around. - Srinivasan, Sriram Advanced Perl Programming
(OReilly, 1997), p. 53
10The new DataPresenter Method
- The user creates a subroutine and passes a
reference to it into the method. - Inside the new method, the subroutine is called
to help filter the data.
11The new DataPresenter Method (slightly revised
by Jim Keenan)
- sub select_rows_adv
- my (self, do_it) _at__
- my data self
- foreach my key (sort keys data)
- unless (reservedkey)
- unless (do_it-gt(datakey))
- delete datakey
-
-
-
- self data
- return self
-
12Using select_rows_adv
- create data presenter object as usual
- my dp1 DataPresenterCensus-gtnew( sourcefile
, \_at_fields, \parameters, index) - write the sub-routine
- sub mine
- my arrayrefshift(_at__)
- my lastarrayref-gt0
- if (last/VASQUEZ/)
- return 1
- elsereturn 0
13Using select_rows_adv
- create the reference to the sub-routine
- my subref\mine create the reference to the
sub - call select_rows_adv
- dp1-gtselect_rows_adv(subref)
- and if you want, print the output
- outputfile 'myfiltered.txt'
- dp1-gtprint_to_file(outputfile)
14Advantages of select_rows_adv
- The user can get as fancy as she wants to and the
DataPresenter code does not have to change. - "Give me all records where the ward number is
between 100 and 300, the last name ends with Z,
the third letter of the first name is A, and
client number plus ward number is greater than
456,900."
15Advantages of select_rows_adv
- sub mine2
- my ( lastname,firstname,cno,unit,ward,
dateadmission,datebirth)_at_shift() - if (wardgt100 and wardlt300 and lastname /Z/
and firstname/..A/ and (wardcnogt456900))
- return 1
- else return 0
-
- my subref\mine2
- dp_object-gtselect_rows_adv(subref)
- dp_object-gtprint_to_file(filtered.txt)
16Advantages of select_rows_adv
- Heres what gets printed in filtered.txt (the
print_to_file method prints semicolon delimited
text) - HERNANDEZSHARON456791SAMSON02172001-07-25196
3-08-01 - VASQUEZJOAQUIN456789SAMSON02091990-11-141970
-03-25
17Advantages of select_rows_adv
- The method resides in the DataPresenter package.
- Because the method uses the field information the
object itself carries around with it, the code
will work on all DataPresenter subclasses. The
programmer doesnt have to add extra methods to
subclasses he creates.
18Disadvantages of select_rows_adv
- Select_rows_adv is not as elegant and simple as
select_rows. - It requires the user to write a sub-routine.
- Probably many other things I havent thought of
yet.
19Conclusions
- Use the easier select_rows when you can.
- When you want to filter in a way not supported by
select_rows, use select_rows_adv. - Callback functions can make a programmers life
easier!
20The End