Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

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!

Comment on text->tagBind() question
Select or Download Code
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 - mneylon-pm@masemware.com || "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.

    --rjray

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://145285]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2014-11-27 06:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (180 votes), past polls