DataPresenter: The Saga Continues

20
DataPresenter: The Saga Continues Part 1: Using Callback Functions to Filter DataPresenter Objects Glenn Maciag [email protected]

description

DataPresenter: The Saga Continues. Part 1: Using Callback Functions to Filter DataPresenter Objects Glenn Maciag [email protected]. Advanced Filtering the Easy Way:. Let the User Do It!. Jim Keenan’s DataPresenter Module. Allows users to manipulate database reports. Sorting - PowerPoint PPT Presentation

Transcript of DataPresenter: The Saga Continues

Page 1: DataPresenter:  The Saga Continues

DataPresenter: The Saga Continues

Part 1: Using Callback Functions to Filter DataPresenter Objects

Glenn Maciag

[email protected]

Page 2: DataPresenter:  The Saga Continues

Advanced Filtering the Easy Way:

Let the User Do It!

Page 3: DataPresenter:  The Saga Continues

December 18, 2001 Perl Seminar NY 3

Jim Keenan’s DataPresenter Module

• Allows users to manipulate database reports.– Sorting– Filtering– Writing to files

• Let’s look at a sample database report.

Page 4: DataPresenter:  The Saga Continues

December 18, 2001 Perl Seminar NY 4

Jim Keenan’s 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

Page 5: DataPresenter:  The Saga Continues

December 18, 2001 Perl Seminar NY 5

Jim Keenan’sDataPresenter Module

• Every type of object has a configuration file. Here is the beginning of one for the DataPresenter::Census class.

@fields = qw(

lastname

firstname

cno

unit

ward

dateadmission

datebirth);

Page 6: DataPresenter:  The Saga Continues

December 18, 2001 Perl Seminar NY 6

Filtering a DataPresenter Object

$column=‘dateofbirth’;

$relation=‘before’;

@choices=(‘01/01/1970’);

$dpobject->select_rows( $column,$relation,\@choices);

Page 7: DataPresenter:  The Saga Continues

December 18, 2001 Perl Seminar NY 7

Filtering 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?

Page 8: DataPresenter:  The Saga Continues

December 18, 2001 Perl Seminar NY 8

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.

Page 9: DataPresenter:  The Saga Continues

December 18, 2001 Perl Seminar NY 9

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

Page 10: DataPresenter:  The Saga Continues

December 18, 2001 Perl Seminar NY 10

The 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.

Page 11: DataPresenter:  The Saga Continues

December 18, 2001 Perl Seminar NY 11

The new DataPresenter 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;

}

Page 12: DataPresenter:  The Saga Continues

December 18, 2001 Perl Seminar NY 12

Using select_rows_adv

#create data presenter object as usual

my $dp1 = DataPresenter::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;}

};

Page 13: DataPresenter:  The Saga Continues

December 18, 2001 Perl Seminar NY 13

Using select_rows_adv

#create the reference to the sub-routine

my $subref=\&mine; #create the reference to the sub

#call select_rows_adv

$dp1->select_rows_adv($subref);

#and if you want, print the output

$outputfile= 'myfiltered.txt';

$dp1->print_to_file($outputfile);

Page 14: DataPresenter:  The Saga Continues

December 18, 2001 Perl Seminar NY 14

Advantages 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."

Page 15: DataPresenter:  The Saga Continues

December 18, 2001 Perl Seminar NY 15

Advantages of select_rows_adv

sub mine2{ 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=\&mine2;

$dp_object->select_rows_adv($subref);

$dp_object->print_to_file(‘filtered.txt’);

Page 16: DataPresenter:  The Saga Continues

December 18, 2001 Perl Seminar NY 16

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;

Page 17: DataPresenter:  The Saga Continues

December 18, 2001 Perl Seminar NY 17

Advantages 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 doesn’t have to add extra methods to subclasses he creates.

Page 18: DataPresenter:  The Saga Continues

December 18, 2001 Perl Seminar NY 18

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.

Page 19: DataPresenter:  The Saga Continues

December 18, 2001 Perl Seminar NY 19

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!

Page 20: DataPresenter:  The Saga Continues

December 18, 2001 Perl Seminar NY 20

The End