derek3000 has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl -w # #lsearch.pl -- GUI app # #to search through log (or just txt) # #files for a pattern. Done because # #NT doesn't have grep. # #-----------------------------------------------# use strict; use Tk; my $mw = MainWindow->new(); #----------Labels & Entries----------* my $f_label = $mw->Label(-text => 'Enter file for searching here:'); $f_label->pack(); my $f_entry = $mw->Entry; my $p_label = $mw->Label(-text => 'Enter pattern to search for here:') +; $p_label->pack(); my $p_entry = $mw->Entry; #------------------------------------* #----------Checkbuttons----------* my $cbI = ''; my $cbS = ''; my $cbX = ''; my $cbO = ''; $mw->Checkbutton(-text => 'ignore case', -variable => \$cbI, -onvalue => 'i', -offvalue => '')->pack(); $mw->Checkbutton(-text => 'periods match newlines', -variable => \$cbS, -onvalue => 's', -offvalue => '')->pack(); $mw->Checkbutton(-text => 'ignore whitespace', -variable => \$cbX, -onvalue => 'x', -offvalue => '')->pack(); $mw->Checkbutton(-text => 'compile RE once only', -variable => \$cbO, -onvalue => 'o', -offvalue => '')->pack(); #------------------------------* #----------Action Buttons----------* my $s_button = $mw->Button(-text => 'Search', -command => \&search(get +_cb_values()) ); $s_button->pack(); my $x_button = $mw->Button(-text => 'Close', -command => sub {exit}); $x_button->pack(); #----------------------------------* #----------Text Area----------* my $output = $mw->Scrolled('Text', -width => 30, -height => 15, -scrollbars => 'se')->pack(); #-----------------------------* MainLoop; #----------Subroutines----------* sub search{ my $m = shift || ''; my $f = $f_entry->get(); my $p = $p_entry->get(); my $results; $p =~ s/([^\w\s\\|()[\]{}^\$~+?.])/\\$1/g; my $re = eval "qr/$p/$m"; die "Eval failed $@\n" if $@; open(FH, $f) or die "Couldn't open that file: $!"; while(<FH>){ if($_ =~ $re){ $results .= $_; } } close FH; display($results); } sub get_cb_values{ my $values = $cbI . $cbS . $cbX . $cbO; die "illegal mods" unless $values =~ m/^[ismxo\S]\z/; return $values; } sub display{ my $stuff = shift; $output->configure(-state => 'normal'); $output->delete('1.0', 'end'); $output->insert('end', "$stuff"); $output->configure(-state => 'disabled'); } #-------------------------------*
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Follow-up on wimpy GUI search script...
by TheoPetersen (Priest) on Jul 10, 2001 at 22:54 UTC |