Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Re: Re: Tk question

by benn (Priest)
on Sep 10, 2003 at 09:49 UTC ( #290353=note: print w/ replies, xml ) Need Help??


in reply to Re: Re: Tk question
in thread Tk question

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.


Comment on Re: Re: Re: Tk question
Select or Download Code
Re^4: Tk question
by PhilHibbs (Hermit) on Sep 10, 2003 at 10:35 UTC
    '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. That is generally a Bad Thing.
    That makes it sound like using "my" at global scope is a Bad Thing. Shurely not?
      Yes, I think globals are a Bad Thing generally - not So Bad that I don't use them {g}, but it'll only ever be a couple ($dbh and $cgi, generally). I try to only use globals if it makes things more stylish, or efficient, or pretty. In a case like this, where it looks he's trying to make *every widget* global, that's almost definitely a Bad Thing, and is probably contributing to some 'code spaghetti'.

      Cheers, Ben.

Re: Re: Re: Re: Tk question
by Anonymous Monk on Sep 10, 2003 at 10:47 UTC
    ...'main body' of your code, it's global...
    Sorry, but it is not. Lexicals do not have global scope. What you are referring to is called file scope.
Re: Re: Re: Re: Tk question
by perl_seeker (Scribe) on Sep 15, 2003 at 09:05 UTC
    Hi :)
    It was $t which was causing the problem.I had declared it using
    my $t = $mw->Scrolled("Text",-font=>"{arial} 12 {bold}")->pack(-side = +> 'bottom', -fill => 'both', -expand => 1);
    i.e. global to the file so these statement later
    for($t=0;$t<$lthree;$t++) { if($mpart=~/$three[$t]/)
    caused the problem.Here I had declared $t without "my".This portion of code was earlier in a separate
    file.I did not notice this bit earlier.
    Thanx :)
Re: Re: Re: Re: Tk question
by perl_seeker (Scribe) on Sep 22, 2003 at 11:25 UTC
    Hi :),
    I posted a reply before, but strangely it hasn't appeared on this page.It was $t causing all the problem.
    Initially $t was declared global to the file:
    my $t = $mw->Scrolled("Text",-font=>"{arial} 12 {bold}")->pack(-side = +> 'bottom', -fill => 'both', -expand => 1);
    Then this bit of code, (which was earleir in a separate file) caused all the trouble.$t was declared without a my.
    for($t=0;$t<$lthree;$t++) {
    Thanx,
    :)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2014-11-21 00:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (103 votes), past polls