Hi, I modified your code to use threads, and I used numbers1..4 as the filenames, which I filled with
#!/usr/bin/perl
use warnings;
use strict;
my $file = shift or die;
open (FH,">$file") or die;
for(1..1000){
my $first = sprintf('%03d', int rand 999);
my $second = sprintf('%03d', int rand 999);
my $third = sprintf('%04d', int rand 9999);
print FH "$first-$second-$third\n";
}
for testing.
Now here is a way to search them for a single number, which stops all the threads when a number is found and reports the line number. Your Tk code could use some minor fixing, but that is up to you. There is also alot of code cleanup and opportunities for ingenious juggling of the return data....like search certain threads only for certain prefixes, or looking for the same number on multiple lists. But I made this as simple as I could to demonstrate the basics... like the timer to watch the shared variables, and how to cleanup the threads when exiting. Also you will need to work out proper messaging and resetting the threads after an empty search
#!/usr/bin/perl
use warnings;
use strict;
use Tk;
use Tk::Dialog;
use threads;
use threads::shared;
my @sfiles = qw(numbers1 numbers2 numbers3 numbers4);
# make threads first before any tk code
my %shash;
my %thash;
my $numworkers = scalar @sfiles;
my $searchnum;
share $searchnum;
$searchnum = '002-339-7473';
foreach my $dthread(1..$numworkers){
share ($shash{$dthread}{'go'});
share ($shash{$dthread}{'die'});
share ($shash{$dthread}{'file'});
share ($shash{$dthread}{'return'});
$shash{$dthread}{'go'} = 0;
$shash{$dthread}{'file'} = shift @sfiles;
$shash{$dthread}{'die'} = 0;
$shash{$dthread}{'return'} = '';
$thash{$dthread}{'thread'} = threads->new(\&work,$dthread);
}
my $count = 1;
my $mw = MainWindow->new;
my $c = $mw->Canvas(
-width =>400,
-height=>200,
-background => "white");
$c->pack;
############################Main window
my $quit = $mw->Button(
-text => "Quit",
-padx => "8",
-command => \&Quit);
$quit->place(-x => 350, -y => 180);
###########################adds the quit button
my $Find = $mw->Button(
-text => "Search",
-command => \&Search);
$Find->place(-x => 300, -y => 180);
################################################
my $lab = $mw->Label (
-text => "Enter Phone Number",
-background => "white"
);
$lab->place(-x => 5, -y => 50);
##############################Label
my $Entry = $mw->Entry(
-textvariable => \$searchnum,
-background => "white"
);
$Entry->place(-x => 5, -y => 68);
##################################Entry
my $text = $mw->Text(
-height => 5,
-width => 55,
-wrap => 'char'
);
$text->place(-x => 5, -y => 90);
#################################
my $labWarning = $mw->Label (
-text => 'Example XXX - XXX - XXXX',
-background => "white"
);
$labWarning->place(-x => 140, -y => 68);
my $index = 1;
#######################################
sub Quit {
my $d = $mw->Dialog(
-text => "Are you sure you want to quit?",
-buttons => ["Yes", "No"]);
my $answer = $d->Show();
close(UNLOCK);
#cleanup threads
if($answer eq "Yes"){
for(1..$numworkers){
$shash{$_}{'die'} = 1;
$thash{$_}{'thread'}->join;
}
exit;
}
}
sub Search {
my $found = 0;
for(1..$numworkers){
$shash{$_}{'go'} = 1;
}
#setup a timer to watch for returns thru shared variables
my $timer; # declare outside of block so you can kill it in callback
$timer = $mw->repeat(10,sub{
for(1..$numworkers){
if( length $shash{$_}{'return'} > 0){
if( $shash{$_}{'return'} > 0 ){
my $message = "thread $_ detected number at line
+$shash{$_}{'return'} \n";
print "$message\n";
$text->insert('end',"$message\n");
}
#stop other threads
for(1..$numworkers){ $shash{$_}{'return'} = 0 }
#stop timer
$timer->cancel;
}
}
});
}
MainLoop;
######################################################################
+#
sub work{
my $dthread = shift;
$|++;
my $file = $shash{$dthread}{'file'};
open(FH,"< $file") or die "$!\n";
print "thread $dthread created\n";
while(1){
if($shash{$dthread}{'die'} == 1){ goto END };
if ( $shash{$dthread}{'go'} == 1 ){
seek FH, 0, 0; #reset to top
$shash{$dthread}{'return'} = '';
print "thread $dthread going\n";
while(my $line = <FH>){
chomp $line;
if($line eq $searchnum){
$shash{$dthread}{'return'} = __LINE__ ;
print "$dthread found $shash{$dthread}{'return'}\
+n";
goto RESET;
}
if($shash{$dthread}{'go'} == 0){goto RESET}
if($shash{$dthread}{'die'} == 1){ goto END };
+
}
RESET:
$shash{$dthread}{'go'} = 0; #turn off self before returni
+ng
print "thread $dthread going back to sleep\n";
}else
{ select(undef,undef,undef,.1) } # tenth second sleep, can be
+ smaller
}
END:
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.