Data Presenter The Saga Continues Part 1 Using

  • Slides: 20
Download presentation
Data. Presenter: The Saga Continues Part 1: Using Callback Functions to Filter Data. Presenter

Data. Presenter: The Saga Continues Part 1: Using Callback Functions to Filter Data. Presenter Objects Glenn Maciag Glenn_Maciag@Kaplan. com December 18, 2001 Perl Seminar NY

Advanced Filtering the Easy Way: Let the User Do It! December 18, 2001 Perl

Advanced Filtering the Easy Way: Let the User Do It! December 18, 2001 Perl Seminar NY

Jim Keenan’s Data. Presenter Module • Allows users to manipulate database reports. – Sorting

Jim Keenan’s Data. Presenter Module • Allows users to manipulate database reports. – Sorting – Filtering – Writing to files • Let’s look at a sample database report. December 18, 2001 Perl Seminar NY 3

Jim Keenan’s Data. Presenter Module CLIENTS - JULY 26, 2001 - C O N

Jim Keenan’s Data. Presenter 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 JONES SMITH THOMPSON VASQUEZ WATSON WELKMAN WILSON SHARON TIMOTHY HAROLD BETTY SUE GEORGE JEFFREY JORGE JOAQUIN JUNE THOMAS SYLVESTER December 18, 2001 456791 803092 359962 698389 786792 906786 456787 456789 456788 456790 498703 SAMSON LAVER TRE LAVER SAMSON LAVER 0217 0103 0111 0211 0104 0111 0105 0209 0107 0110 Perl Seminar NY 2001 -07 -25 2001 -03 -19 2001 -07 -19 1992 -01 -23 2001 -07 -26 2001 -07 -15 1986 -01 -17 1990 -11 -14 1990 -08 -23 1980 -11 -14 1983 -04 -02 1963 -08 -01 1969 -06 -29 1973 -10 -02 1949 -08 -12 1973 -08 -17 1953 -02 -28 1956 -01 -13 1970 -03 -25 1970 -15 -23 1960 -14 -02 1953 -06 -22 4

Jim Keenan’s Data. Presenter Module • Every type of object has a configuration file.

Jim Keenan’s Data. Presenter Module • Every type of object has a configuration file. Here is the beginning of one for the Data. Presenter: : Census class. @fields = qw( lastname firstname cno unit ward dateadmission datebirth); December 18, 2001 Perl Seminar NY 5

Filtering a Data. Presenter Object $column=‘dateofbirth’; $relation=‘before’; @choices=(‘ 01/01/1970’); $dpobject->select_rows( $column, $relation, @choices); December

Filtering a Data. Presenter Object $column=‘dateofbirth’; $relation=‘before’; @choices=(‘ 01/01/1970’); $dpobject->select_rows( $column, $relation, @choices); December 18, 2001 Perl Seminar NY 6

Filtering a Data. Presenter Object • Advantages – Elegant, natural language interface – Easy

Filtering a Data. Presenter Object • Advantages – Elegant, natural language interface – Easy for user to work with • Disadvantage – What if a user wants to enter a regular expression? December 18, 2001 Perl Seminar NY 7

Advanced Filtering: My First Thought • User can pass in to the new method

Advanced Filtering: My First Thought • User can pass in to the new method a compiled regex along with its target $dpobject->select_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. December 18, 2001 Perl Seminar NY 8

Filtering using Callback Functions • “A callback function is an ordinary subroutine whose reference

Filtering using Callback Functions • “A callback function is an ordinary subroutine whose reference is passed around. ” – Srinivasan, Sriram Advanced Perl Programming (O’Reilly, 1997), p. 53 December 18, 2001 Perl Seminar NY 9

The new Data. Presenter Method • The user creates a subroutine and passes a

The new Data. Presenter 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. December 18, 2001 Perl Seminar NY 10

The new Data. Presenter Method (slightly revised by Jim Keenan) sub select_rows_adv { my

The new Data. Presenter Method (slightly revised by Jim Keenan) sub select_rows_adv { my ($self, $do_it) = @_; my %data = %$self; foreach my $key (sort keys %data) { unless ($reserved{$key}) { unless ($do_it->($data{$key})) { delete $data{$key}; } } } %$self = %data; return $self; } December 18, 2001 Perl Seminar NY 11

Using select_rows_adv #create data presenter object as usual my $dp 1 = Data. Presenter:

Using select_rows_adv #create data presenter object as usual my $dp 1 = Data. Presenter: : Census->new( $sourcefile, @fields, %parameters, $index); #write the sub-routine sub mine{ my $arrayref=shift(@_); my $last=$arrayref->[0]; if ($last=~/VASQUEZ/){ return 1; }else{return 0; } }; December 18, 2001 Perl Seminar NY 12

Using select_rows_adv #create the reference to the sub-routine my $subref=&mine; #create the reference to

Using select_rows_adv #create the reference to the sub-routine my $subref=&mine; #create the reference to the sub #call select_rows_adv $dp 1 ->select_rows_adv($subref); #and if you want, print the output $outputfile= 'myfiltered. txt'; $dp 1 ->print_to_file($outputfile); December 18, 2001 Perl Seminar NY 13

Advantages of select_rows_adv • The user can get as fancy as she wants to

Advantages of select_rows_adv • The user can get as fancy as she wants to and the Data. Presenter 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. " December 18, 2001 Perl Seminar NY 14

Advantages of select_rows_adv sub mine 2{ my ( $lastname, $firstname, $cno, $unit, $ward, $dateadmission,

Advantages of select_rows_adv sub mine 2{ my ( $lastname, $firstname, $cno, $unit, $ward, $dateadmission, $datebirth)=@{shift()}; if ($ward>100 and $ward<300 and $lastname =~/Z$/ and $firstname=~/^. . A/ and ($ward+$cno>456900)){ return 1; } else { return 0; } }; my $subref=&mine 2; $dp_object->select_rows_adv($subref); $dp_object->print_to_file(‘filtered. txt’); December 18, 2001 Perl Seminar NY 15

Advantages of select_rows_adv • Here’s what gets printed in filtered. txt (the print_to_file method

Advantages of select_rows_adv • Here’s what gets printed in filtered. txt (the print_to_file method prints semicolon delimited text): HERNANDEZ; SHARON; 456791; SAMSON; 0217; 2001 -07 -25; 1963 -08 -01; VASQUEZ; JOAQUIN; 456789; SAMSON; 0209; 1990 -11 -14; 1970 -03 -25; December 18, 2001 Perl Seminar NY 16

Advantages of select_rows_adv • The method resides in the Data. Presenter package. – Because

Advantages of select_rows_adv • The method resides in the Data. Presenter package. – Because the method uses the field information the object itself carries around with it, the code will work on all Data. Presenter subclasses. The programmer doesn’t have to add extra methods to subclasses he creates. December 18, 2001 Perl Seminar NY 17

Disadvantages of select_rows_adv • Select_rows_adv is not as elegant and simple as select_rows. •

Disadvantages 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 haven’t thought of yet. December 18, 2001 Perl Seminar NY 18

Conclusions • Use the easier select_rows when you can. • When you want to

Conclusions • 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 programmer’s life easier! December 18, 2001 Perl Seminar NY 19

The End December 18, 2001 Perl Seminar NY 20

The End December 18, 2001 Perl Seminar NY 20