Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

text->tagBind() question

by rbc (Curate)
on Feb 13, 2002 at 21:05 UTC ( #145285=perlquestion: print w/replies, xml ) Need Help??
rbc has asked for the wisdom of the Perl Monks concerning the following question:

Hi fellow Monks,

I wonder if I am doing somthing wrong or is this just
the way tag's in Tk::Text work.
The documentation defines tagBind like so ...

$text->tagBind(tagName, ?sequence?, ?script?)

Please take a look at these code snippets and let me know
if am doing something that is causing my problem

The problem I am dealing with is that when I specify a ?script? like so ...
sub myTagCallback { my $theTextWidget = shift; # I don't know why this shows up! my $str = shift; my $arrRef = shift; my $tableWidget = shift; . . . } . . . $myTextWidget->tagBind ( $myTagName, "<ButtonRelease-1>", [\&myTagCallback, $myString, \@myArrayOArrays, $myTableWidget] ); . . .

What I don't understand is how/why I have to do the ...
my $theTextWidget = shift; # I don't know why this shows up!
... I didn't specify it as an argument in the tagBind call !?!?!?!

Please enlighten me. Thanks!

Replies are listed 'Best First'.
Re: itext/i-tagBind() question
by Masem (Monsignor) on Feb 13, 2002 at 22:10 UTC
    This has to do with how Perl handles object oriented calls. (See perltoot for a starting point). Perl handle functions called on an object as calling the function in the object's class' namespace, placing the object at the start of the argument list for the function. Basically, when you write:
    $myobject->do_something( $arg1, $arg2, @rest );
    Perl reparses this as:
    do_something( $myobject, $arg1, $arg2, @rest );
    and then otherwise treats this as a normal function. Thus, to get the actual object that you are working on, typically called "$self", you simple shift out the first argument that is passed to that function.

    So you're doing everything correctly; that first line is basically necessary to work with objects in perl.

    Dr. Michael K. Neylon - || "You've left the lens cap of your mind on again, Pinky" - The Brain
    "I can see my house from here!"
    It's not what you know, but knowing how to find it if you don't know that's important

Re: itext/i-tagBind() question
by rjray (Chaplain) on Feb 13, 2002 at 22:17 UTC
        What I don't understand is how/why I have to do the ...
            my $theTextWidget = shift; # I don't know why this shows up!
        ... I didn't specify it as an argument in the tagBind call !?!?!?!

    The reason for this is simple: all Tk callbacks pass the object reference as the first argument. Otherwise, your callback has no ready means by which to know which of the potentially many widgets was activated. It is in effect treating the callbacks as methods on the objects themselves. Just as any other class-method receives the object reference first, so do these.

    While it may not be convenient if you have no plans to use the object itself, it's a realtively minor inconvenience overall. You can simply shift without assigning it, if you have no need for it.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://145285]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (9)
As of 2018-06-18 16:31 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (110 votes). Check out past polls.