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

I've got a sub within my Tk called 'quit'. What it's supposed to do is take the first argument supplied to it, and destroy whatever it is. The sub code is:
sub quit { $_[0]->destroy; }
When I call it, I have:
&quit('$windowname')
However, it is giving me an error. Does anyone know how I could do this without creating a sub for everything I want to destroy?

Replies are listed 'Best First'.
Re: Dynamically destroying a window in Tk?
by Ytrew (Pilgrim) on Dec 05, 2004 at 23:24 UTC
    Well, first of all, without knowing if your Perl/TK call to destroy works, I can see that your call to quit() has problems.

    You shouldn't use single quotes, or you'll pass the literal string '$windowname' to your function. It looks like you want to pass in the value of the $windowname variable instead. Remove the single quotes from the function call, and try again. :-)

    Good luck!

    --
    Ytrew Q. Uiop

      I tried to do that, and it didn't work. I'm trying to pass the actual variable, not it's contents, so that I can use destroy on the variable. If that made any sense.
Re: Dynamically destroying a window in Tk?
by Koosemose (Pilgrim) on Dec 05, 2004 at 23:19 UTC

    Just to be on the safe side, you might want to add what error you're getting, It could aid in helping getting things to work right.

    A couple of off handed guesses though, You might need another level of indirection (an extra $ and some curly braces perhaps) It would seem like what you're doing is creating an an indirect reference in effect. Perhaps better than trying to get past levels of indirection and such, you could pass the object itself. I think it would be achieved by passing a reference

     &quit(\$windowname);

    But besides that I'll leave it to wiser monks than myself, or at least wait for an error message :)

    Good Luck.

    Just Another Perl Alchemist
Re: Dynamically destroying a window in Tk?
by Mr. Muskrat (Canon) on Dec 06, 2004 at 02:30 UTC

    Usually when doing something like this, you are going to use a method.

    $windowname->quit;

Re: Dynamically destroying a window in Tk?
by Spidy (Chaplain) on Dec 05, 2004 at 23:25 UTC
    Well, here's the error message(s):
    Can't call method 'destroy' without a package or object reference at * +.pl line *. Uncaught exception from user code: Can't call method 'destroy' without a package or object reference at * +.pl line *. main::quit('windowname') called at *.pl line *.

      Yep, A reference allows you to pass the actual variable (contents and all, which is what you need) With the single quotes, you're basically passing an indirect reference (though not quite in the way that more or less works) and that's generally considered a Bad Thing. Try passing a real refernce as I mentioned in my last post, and let us know how/if that works.

      I believe the sub will need altered a touch to, this should do it:

      sub quit { $$_[0]->destroy; }
      Just Another Perl Alchemist
        Alright, I've messed around with it a bit, and managed to get it down to this:
        MainWindow=HASH(0x164a81c) is not a widget at *.pl line **.


        Any more ideas?