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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Calvin

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

    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('roulet teTwoPoint', 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.
  • Joe Smith

    #2
    Re: Does Perl have anything that equates to the C++ this pointer?

    Calvin wrote:
    [color=blue]
    > 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.[/color]

    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

    Comment

    • Calvin

      #3
      Re: Does Perl have anything that equates to the C++ this pointer?

      Joe Smith <Joe.Smith@inwa p.com> wrote in message news:<UuSOc.188 251$a24.171398@ attbi_s03>...[color=blue]
      >
      > 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[/color]

      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

      Comment

      Working...