Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Tk question

by perl_seeker (Scribe)
on Sep 08, 2003 at 11:40 UTC ( [id://289743]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Perl Monks,
I have a perl script which makes use of Tk
main.pl use Tk; . . . . sub s1 { require p1.pl; require p2.pl; require p3.pl; require p4.pl; . . . .} 1; . . .
p1.pl,p2.pl,p3.pl,p4.pl contain subroutines which are called within subroutine s1.

As a further level of nesting,in p3.pl
p3.pl . . . sub s2 { require p5.pl; require p6.pl; require p7.pl; require p8.pl; . . . } 1; . . .
p5.pl,p6.pl,p7.pl,p8.pl contain subroutines which are called within subroutine s2.

I had written all the code in parts, i.e. all the lower level subs first and then combined
the entire code using require statements.

The code was working fine this way.When I put all the subroutines in main.pl itself and removed
the require statements, I got a lot of Tk errors:

Tk::Error: Can't call method "insert" without a package or object reference at C:\monisha\project\Appliw\s.pl line
124, <EDINP> line 1.
\&main::replace1
(menu invoke)

Tk::Error: Tk::Label=HASH(0x23b8818) is not a Tk object at D:/Perl/site/lib/Tk.pm line 91, <EDINP> line 1.
Tk callback for tkwait
(command bound to event)

Tk::Error: Can't call method "get" without a package or object reference at C:\monisha\project\Appliw\s.pl line 46,
<EDINP> line 1.
\&main::tged

Line 124 is this:

$t->insert('sel.first',"$chosen1");

Line 46:

@chars=$t->get('sel.first','sel.last');

What is the problem here?
Please help.Thanx
:)

Replies are listed 'Best First'.
Re: Tk question
by benn (Vicar) on Sep 08, 2003 at 12:49 UTC
    The error messages speak for themselves. In line 46/124, $t doesn't appear to be defined at all, and it looks like there's something similar causing the "Not a Tk Object" error.

    My best guess is variable clash - removing all those requires was a *good* thing (but check out modules and the use statement sometime soon), but I reckon there are variables in the subs called $t somewhere that aren't localised...are you using strict & warnings? If not, do. They should be able to help you catch errors like this.

    Cheers, Ben.

      Hi,
      thanks for the reply.I am a bit confused.In the first section of code I have a text widget defined like this:
      my $f = $mw->Frame->pack(-side => 'top', -fill => 'x'); my $t = $mw->Scrolled("Text",-font=>"{arial} 12 {bold}")->pack(-side = +> 'bottom', -fill => 'both', -expand => 1);
      Then I have a number of subroutines where $t is used again. These subroutines are bound to buttons which
      are defined in the frame above with stmts like:
      $f->Button(-text => "Display", -command =>\&tged)->pack(-side => 'righ +t'); sub tged { @chars=$t->get('sel.first','sel.last'); #print "\nSelected word:"; #print @chars; } 1;
      In the sub &tged, the get stmt should read characters within the text widget defined initially using $t.If I use
      "my $t" within this sub and localise it, we wont be referring to the first $t defined outside the sub.Is that right?

      These subs also use $t again and need to refer to the text widget define initially using:
      my $t = $mw->Scrolled("Text",-font=>"{arial} 12 {bold}")->pack(-side = +> 'bottom', -fill => 'both', -expand => 1); sub replace1{ . . $t->insert('sel.first',"$chosen1"); . . } 1; sub replace2{ . . $t->insert('sel.first',"$chosen2"); . . } 1; sub replace3{ . . $t->insert('sel.first',"$chosen3"); . . } 1; sub addit{ . . @addword=$t->get('sel.first','sel.last'); . . } 1;
      I'm not sure where we should use "my", and where we should not.Turning on use strict gives no message about $t.
      The sub &tged works on the first button click, and does not work therafter.The other subs are not working at all.
      Please help.
      Thanx :)
        OK - maybe my first guess was wrong... :)

        If I use "my $t" within this sub and localise it, we wont be referring to the first $t defined outside the sub.Is that right?
        Yes - that's right. I thought maybe you were using $t for some other purpose, but it would appear not.

        I'm not sure where we should use "my", and where we should not
        Check out the section on 'scope' in whatever perl books/docs you have. 'my' declares a variable within the 'current scope' - if that's in a sub (or any pair of braces),it's gone when you exit the sub. If, on the other hand, it's in the 'main body' of your code, it's global oops - I mean file scoped...ermm..global to your file...ermm...you know what I mean - see the AM posting below... . That is generally a Bad Thing.

        Turning on use strict gives no message about $t.
        Does it give messages about other things? What about use warnings? Fix these, and your problem may possibly go away.

        I haven't any more specific ideas, but here are a few general suggestions.

        • use strict; use warnings;
        • Don't use globals if you can help it. Pass arguments to subroutines instead - this helps your code become more 'general'
        • use strict; use warnings;
        • Refactor and tidy up. sub replace1,sub replace2 etc. look like they should be candidates for a single sub, with the replace string (and probably the text widget as well) passed in as a parameter.
        • use strict; use warnings;
        • Debug. Lots. Print out the values of $t after you initialise it, before you use it, after you use it etc. Check they're all the same type/memory address.
        Hope this helps, Ben.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-03-28 15:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found