Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

OK...forgive this dumb question... In C++ and Java, I've been accustomed to using the "this" qualifier to specify my objects. How would this happen in Perl? The code in question:
$accountName = ""; $count = 0; $top = 5; $left = 15; foreach my $row (1..222){ foreach my $col (1..5){ if ($col eq 1 && $accountName !~ $Sheet->Cells($row,$col)->{'V +alue'}){ $accountName = $Sheet->Cells($row,$col)->{'Value'}; $accounts[$count] = $accountName; if ($count eq 9 || $count eq 18) { $left += 150; $top = 5; } $labels[$count] = $main->AddLabel( -text => $accountName, -top => $top, -left => $left ); $top += 15; $comboboxes[$count] = $main->AddCombobox( -name => $accountName."ComboBox", -top => $top, -left => $left, -width => 125, -height => 150, -tabstop => 1, -style => WS_VISIBLE | 3 | WS_VSCROLL ); $count++; $top += 25; } else { if ($col eq 2){ $value = $Sheet->Cells($row,$col)->{'Value'}; $comboboxes[$count-1]->InsertItem($value); } } } } $Book->Close; sub ComboBox{ #Help here - this.$selection = GetString(SelectedItem); ??? } foreach my $x (0..$count){ $SUB = $accounts[$x]."ComboBox_Change"; *$SUB = \&ComboBox; }
Yes, it's been cleaned a bit and optimized since my last post... thanks guys. My problem is in the "ComboBox" function, I want to do a getstring on whichever combobox made the call... Thanks again. Josh Pavel

Replies are listed 'Best First'.
Re: "this" variable?
by Abigail-II (Bishop) on May 30, 2002 at 18:53 UTC
    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

      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?
Re: "this" variable?
by jsprat (Curate) on May 30, 2002 at 21:07 UTC
    I used to do VB, and what you are trying to do should be simple. In VB, you can create an array of combo boxes, and the first argument to ComboBox_Change would be the index of the current combo box. This is standard Windows functionality.

    Sadly (in your case), control arrays are not made available to the Perl programmer through Win32::GUI. When the ComboBox_Change event is called, it is called with no arguments at all.

    To make it work, I would create an anonymous sub reference like so:

    foreach my $x (0..$count){ $SUB = accounts[$x]."ComboBox_Change"; *$SUB = sub { ComboBox $x }; }

    Then in sub ComboBox, put

    my $index = shift;

    This will pass the array index of the changed combo box to the ComboBox sub, which you can use with comboboxes[$index]

    Note: This will not work with strict unless you use no strict qw/refs/;, but since Win32::GUI itself won't run properly without no strict qw/subs/; this shouldn't trouble you too much.