in reply to "this" variable?

I strongly suggest you look at the various manual pages dealing with objects, like perlobj, perltoot, perlboot and even perlmod.

There you will see that the first argument of a method is the object on which the method is called. So, if you begin your methods with

my $this = shift;
then $this will contain the current object.

Abigail

Replies are listed 'Best First'.
Re: Re: "this" variable?
by tomhukins (Curate) on May 30, 2002 at 19:19 UTC
    It's worth noting that in Perl the convention is to use $self to represent the current object. You can use $this or $whatever_you_want, but people who are used to reading Perl will find $self self-explanatory (please, excuse the pun).
      this just isn't working for me... if I do a $self = shift; in the beginning of that sub function, my $self value is blank. Of course, then, the call
      $selection = $comboboxes[$self]->GetString($comboboxes[$self]->Selecte +dItem);
      fails. My code currently is this:
      sub ComboBox{ $self = shift; #print "In ComboBox - $self"; $selection = $comboboxes[$self]->GetString($comboboxes[$self]->Sel +ectedItem); #print $selection; } foreach my $x (0..$count){ $SUB = $accounts[$x]."ComboBox_Change"; *$SUB = \&ComboBox; }
      If those print lines weren't commented out, the output is "In ComboBox - ". Is my logic wrong here, or just my coding? Is there an easier solution I'm overlooking that would still allow for my arrays to grow with the spreadsheet?

        You need to show us where and how you call the &ComboBox subroutine. Follow Abigail-II's advice and read the appropriate man pages.

        Rather than sit around and try and second guess what you're doing, I figured I could show you what is going on here. For a quick example of OO in Perl and what is going on here, save the following file as 'Counter.pm':

        package Counter; use strict; use Carp; sub new { my ($class,$value) = @_; $value ||= 0; my $self = bless {}, $class; croak "Argument to constructor must be positive integer." if ! $self->_good_value( $value ); $self->{_value} = $value; return $self; } sub value { my $self = shift; $self->{_value} = $_[0] if $self->_good_value($_[0]); return $self->{_value}; } sub increment_value { my $self = shift; $self->{_value}++; } sub decrement_value { my $self = shift; $self->{_value}--; } sub _good_value { my ( $self, $value ) = @_; return 0 if ! defined $value; $value =~ /^\d+$/; } 1;

        And then, in the same directory:

        #!/usr/bin/perl -w use strict; BEGIN { unshift @INC, '.'; # add current directory to include path use Counter; } my $counter = Counter->new; print $counter->value, "\n"; $counter->increment_value; print $counter->value, "\n"; $counter->decrement_value; print $counter->value, "\n"; $counter->value(3); print $counter->value, "\n";

        The above code is rather simple and not how I would really do it, but it gives you an idea of what is going on. If you don't understand it, you really need to learn Perl's OO system, as what I have presented is very basic.

        Cheers,
        Ovid

        Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

        There could be many things wrong with the code (which I cannot comment on because I don't see all of it). But a few points we can see:
        • You are using $self = shift;, and you are not putting a my in front of it. That means you are using a global variable. If you do the same in SelectedItem or GetString you will get problems, as they will overwrite what is in $self.
        • You are using $self as an index into an array. But $self is an object. If you use an object as a number, most of the time, you will get a pretty big number. Perl will happely extend the array @comboboxes for you, but you're likely to run out of memory before Perl has extended the array far enough.

        Abigail