Does Perl have anything that equates to the C++ this pointer?

C

Calvin

use AI::Genetic;
my $ga = new AI::Genetic( -fitness => sub { rand },
-type => 'bitvector',
-population => 500,
-crossover => 0.89,
-mutation => 0.01,
-terminate => sub {
$ga->getFittest->score() > 0.9 } );
$ga->init(10);
$ga->evolve('rouletteTwoPoint', 100);
print "Best score = ", $ga->getFittest->score(), "\n";

The above code is taken from the man page for AI::Genetic (with a bit
of cleanup and modification).

What I'm trying to do is replace "sub { rand }" with something more
meaningful. But to do that I need to be able to say something, in
C++, to the effect of "this.genes()". Any help that could be provided
would be greatly apreciated.
 
J

Joe Smith

Calvin said:
What I'm trying to do is replace "sub { rand }" with something more
meaningful. But to do that I need to be able to say something, in
C++, to the effect of "this.genes()". Any help that could be provided
would be greatly apreciated.

Your subject line is very confusing. I assume your use of 'this' in
the subject is not the same as the regular English definition of the word.

sub foo {
my $this = shift; # Pointer to self
my $arg = shift; # First argument given to method call
$this->genes($arg); # Call another method in the same class
}

-Joe

P.S. Next time, post to comp.lang.perl.misc instead of comp.lang.perl
 
C

Calvin

Joe Smith said:
Your subject line is very confusing. I assume your use of 'this' in
the subject is not the same as the regular English definition of the word.

sub foo {
my $this = shift; # Pointer to self
my $arg = shift; # First argument given to method call
$this->genes($arg); # Call another method in the same class
}

-Joe

P.S. Next time, post to comp.lang.perl.misc instead of comp.lang.perl

Thanks for the help. As it turns out there was an undocumented
variable that's passed into the function in question that does the job
much more efficiently than the work-around I was thinking of.

Calvin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top