E
Eric
Hello,
I have a Perl script that utilizes Perl threads to run a function
(time) on a list of entities. Using Perl threads will allow the code
to run in parallel (as opposed to serially) and therefore minimize the
time it takes to run through the list.
The code does something similar to what the following code is doing:
============= Perl script =============
#!/usr/bin/perl
use strict;
use warnings;
use threads;
use threads::shared;
my @clocks = ("clock1", "clock2", "clock3");
foreach my $clock (@clocks) {
my $thr = threads->new(\&get);
sleep 1;
}
for my $t (threads->list()) {
my $result = $t->join;
}
sub get {
my $time = shift;
$time = time();
print STDOUT "The time is: $time\n";
return;
}
=======================================
Now, I want to get this script working in object oriented code. The
following scripts (plagiarized from a Perl book for the sake of
correctness) are doing something similar as the code above, but is
running the time function on the entities in serial, not parallel:
The Perl script is:
============= Perl script =============
#!/usr/bin/perl
use strict;
use warnings;
use Clock;
my @clocks = ("clock1", "clock2", "clock3");
foreach my $clock (@clocks) {
my $clock = new Clock();
sleep 1;
print STDOUT "\$clock is: ". $clock->get(), "\n";
}
=======================================
And the Perl module being used is:
============= Perl module: Clock.pm =============
#!/usr/bin/perl
package Clock;
use strict;
use warnings;
sub new {
my ($type) = @_;
my $self = {};
$self->{time} = time();
bless $self, $type;
}
sub get {
my ($self) = shift;
return($self->{time});
}
1;
=======================================
Note the following line of code in the former (non OO) script:
my $thr = threads->new(\&get);
This line is calling the 'get' sub. So, if I were to use the OO
scripts shown, how should I implement this?
I tried doing the obvious, which was to change the line to:
my $thr = threads->new($clock->get());
But I got the following error:
thread failed to start: Undefined subroutine &main::1177952602 called
at ./CC.pl line 19.
Other failed attempts were:
my $thr = threads->new(\&clock->get());
my $thr = threads->new(Clock::clock->get());
my $thr = threads->new($Clock::get());
Truthfully, I am not well versed at Object Oriented Perl (or anything
else). So at this point I was making guesses that weren't so educated.
Does anyone have a solution?
Thanks in advance to all that respond.
Eric
I have a Perl script that utilizes Perl threads to run a function
(time) on a list of entities. Using Perl threads will allow the code
to run in parallel (as opposed to serially) and therefore minimize the
time it takes to run through the list.
The code does something similar to what the following code is doing:
============= Perl script =============
#!/usr/bin/perl
use strict;
use warnings;
use threads;
use threads::shared;
my @clocks = ("clock1", "clock2", "clock3");
foreach my $clock (@clocks) {
my $thr = threads->new(\&get);
sleep 1;
}
for my $t (threads->list()) {
my $result = $t->join;
}
sub get {
my $time = shift;
$time = time();
print STDOUT "The time is: $time\n";
return;
}
=======================================
Now, I want to get this script working in object oriented code. The
following scripts (plagiarized from a Perl book for the sake of
correctness) are doing something similar as the code above, but is
running the time function on the entities in serial, not parallel:
The Perl script is:
============= Perl script =============
#!/usr/bin/perl
use strict;
use warnings;
use Clock;
my @clocks = ("clock1", "clock2", "clock3");
foreach my $clock (@clocks) {
my $clock = new Clock();
sleep 1;
print STDOUT "\$clock is: ". $clock->get(), "\n";
}
=======================================
And the Perl module being used is:
============= Perl module: Clock.pm =============
#!/usr/bin/perl
package Clock;
use strict;
use warnings;
sub new {
my ($type) = @_;
my $self = {};
$self->{time} = time();
bless $self, $type;
}
sub get {
my ($self) = shift;
return($self->{time});
}
1;
=======================================
Note the following line of code in the former (non OO) script:
my $thr = threads->new(\&get);
This line is calling the 'get' sub. So, if I were to use the OO
scripts shown, how should I implement this?
I tried doing the obvious, which was to change the line to:
my $thr = threads->new($clock->get());
But I got the following error:
thread failed to start: Undefined subroutine &main::1177952602 called
at ./CC.pl line 19.
Other failed attempts were:
my $thr = threads->new(\&clock->get());
my $thr = threads->new(Clock::clock->get());
my $thr = threads->new($Clock::get());
Truthfully, I am not well versed at Object Oriented Perl (or anything
else). So at this point I was making guesses that weren't so educated.
Does anyone have a solution?
Thanks in advance to all that respond.
Eric