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

In new() for one class(list_box) I'm creating a sub-object from a different class (txt_box) and am passing it a (list_box) method reference called "updt" like this
$self->{txt_box}=txt_box->new( $self->{frm}, sub {$self->updt} );
My problem is that when I call the reference from inside the txt_box object as shown below the supplied argument "$self->{contents}" isn't showing up at all. By contrast if I supply a reference to non-oop sub like this...
$self->{txt_box}=txt_box->new( $self->{frm}, \&test_if_txt_box_content +s_passed_back );
then "$self->{contents}" is passed fine. Here's how I'm calling the references
sub new { my $class = shift; my $self = {}; bless( $self, $class ); +#std line $self->{parent_widg}=shift; $self->{sub_to_call_on_keypress}=shift; <===================LOOK + HERE $self->{contents}=''; $self->{entry}=$self->{parent_widg}->new_ttk__entry( -width => 50, + -textvariable => \$self->{contents}, ); $self->{entry}->g_pack( -expand => 0, -fill => 'x', -side => top) +; #-fill => 'both' globals::new_bindtag('new_tag',$self->{entry},4); Tkx::bind( 'new_tag', "<Key>", [ sub { $self->upon_keypress(@_) }, + Tkx::Ev("%W","%K","%k") ] ); return $self; } #std line sub upon_keypress { #calls the sub/method you supplied...if an ordinar +y sub you supply \&sub_nm if method then just obj->method_nm my ($self, $win, $key_nm, $key_no) = @_; #tick print "in upon_keypress and so far you've got $self->{contents}\n +"; #tick $self->{sub_to_call_on_keypress}->( $self->{contents} ); #bug her +e...$self->{contents} gets passed to ORDINARY SUB but not updt method + <===================LOOK HERE }
I'm just wondering why the method isn't "seeing" that argument. Any help much appreciated.

Replies are listed 'Best First'.
Re: unable to call a method reference with an argument
by NetWallah (Canon) on Dec 07, 2015 at 21:59 UTC
    I think the $self->{contents} IS being passed, but is not consumed in the anon sub.

    Try:

    $self->{txt_box}=txt_box->new( $self->{frm}, sub {$self->updt(@_)} # Pass incoming parms along. );

            Our business is run on trust. We trust you will pay in advance.

      The (@_) in "sub {$self->updt(@_)}" worked perfectly. I did try () and ('') to try to force recognition of a single parameter prototype but that didn't work at all and I "solved" the problem by just getting hold of self->(txt_box)->{contents} but wanted to know how to do this for when I can't get at things that way. Thank you very much indeed.
Re: unable to call a method reference with an argument
by kennethk (Abbot) on Dec 07, 2015 at 21:48 UTC
    Your problem description is incomplete insofar as you do not show us test_if_txt_box_contents_passed_back or updt. See How do I post a question effectively?. In particular, creating a self-contained example would be illuminative.

    My guess would be that the $self variable you are calling update on in your sub is a different variable than the $self object that contains contents. Think about it this way:

    $main_self->{txt_box}=txt_box->new( $main_self->{frm}, sub {$main_self +->updt} ); ... sub new { my $class = shift; my $new_self = {}; bless( $new_self, $c +lass ); #std line $new_self->{parent_widg}=shift; $new_self->{sub_to_call_on_keypress}=shift; <=================== +LOOK HERE $new_self->{contents}=''; $new_self->{entry}=$new_self->{parent_widg}->new_ttk__entry( -widt +h => 50, -textvariable => \$new_self->{contents}, ); $new_self->{entry}->g_pack( -expand => 0, -fill => 'x', -side => +top); #-fill => 'both' globals::new_bindtag('new_tag',$new_self->{entry},4); Tkx::bind( 'new_tag', "<Key>", [ sub { $new_self->upon_keypress(@_ +) }, Tkx::Ev("%W","%K","%k") ] ); return $new_self; } #std line sub upon_keypress { #calls the sub/method you supplied...if an ordinar +y sub you supply \&sub_nm if method then just obj->method_nm my ($new_self, $win, $key_nm, $key_no) = @_; #tick print "in upon_keypress and so far you've got $new_self->{contents +}\n "; #tick $new_self->{sub_to_call_on_keypress}->( $new_self->{contents} ); +#bug here...$self->{contents} gets passed to ORDINARY SUB but not upd +t method <===================LOOK HERE }
    Your keypress routine uses $main_self in what is passed, not $new_self that has the contents field.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      "My guess would be that the $self variable you are calling update on in your sub is a different variable than the $self object that contains contents." Yes but they should be. The suggested addition of "(@_)" as in... $self->{txt_box}=txt_box->new( $self->{frm}, sub {$self->updt(@_)} ); has fully solved the problem. Thank you for your suggestions.
        Just to clarify... The $self that goes with updt is implicit in the value of the attribute $self->{sub_to_call_on_keypress} whereas the $self explicit in the the above attribute is the SAME self that is also explicit in the attribute $self->{contents}