Gossamer Forum
Home : General : Perl Programming :

Passing Variables in Perl/Tk???

Quote Reply
Passing Variables in Perl/Tk???
Hello, all. I'm trying to pass a variable to one of my subroutines when my users hit a button, but it always complains. Something about an "undefined subroutine".

Here's my code for the -command attribute of the button:

-command => \&subroutine("variable_to_be_passed"),

sub subroutine
{
# Do stuff with the variable here...
}

If you need additional information about my program, let me know. I'm a newbie, so be gentle when you reply. Wink Also - if you didn't catch the thread title, this is in Perl/Tk.


Thanks for any help!
Furry

P.S. This forum is awesome. Love the smileys and icons and especially spell check. Smile
Quote Reply
Re: [Fat_N_Furry] Passing Variables in Perl/Tk??? In reply to
You should consider using CGI.pm to pass parameters from your form to your script.
========================================
Buh Bye!

Cheers,
Me
Quote Reply
Thanks for replying! In reply to
Well, I would except I'm not trying to pass it from a form. I'm trying to pass the variables from one part of my program to a sub. Any suggestions?

Thanks again for your reply.


Furry
Quote Reply
Re: [Fat_N_Furry] Thanks for replying! In reply to
Can you do

perl -c filename.pl

on your file, and tell us what the output is?

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [Fat_N_Furry] Thanks for replying! In reply to
Quote:
Something about an "undefined subroutine".

You need to be specific in order to receive accurate assistance :)
Quote Reply
Re: [yogi] Thanks for replying! In reply to
In Reply To:
Can you do

perl -c filename.pl

on your file, and tell us what the output is?

calculator.txt syntax OK

Thanks for showing me that, though. I had no idea that perl had a debugging option.

In Reply To:
Quote:
Something about an "undefined subroutine".

You need to be specific in order to receive accurate assistance :)

Tk::Error: Undefined subroutine &main::1 called at c:/gnu_programs/Perl/site/lib/Tk.pm line 228.
\1
Tk callback for .button
Tk::__ANON__at c:/gnu_programs/Perl/site/lib/Tk.pm line 228
Tk::Button::butUp at c:/gnu_programs/Perl/site/lib/Tk/Button.pm line 111
<command bound to event>

Specific enough for ya? Wink


Thanks for all your time,
Furry
Quote Reply
Re: [Fat_N_Furry] Thanks for replying! In reply to
Could you post a link to a .txt version of the script (i.e something that wont be parsed as a .cgi script)?

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Thanks for replying! In reply to
Sure - here you go - this is an example of what I'm trying to do. The reason I don't post the actual file is that I changed the code to get around not being able to pass variables to subroutines. (I have a different subroutine for each button Unimpressed)

Last edited by:

Fat_N_Furry: Dec 11, 2002, 11:24 AM
Quote Reply
Re: [Fat_N_Furry] Thanks for replying! In reply to
So where are MainWindow and MainLoop defined?
Quote Reply
Re: [Paul] Thanks for replying! In reply to
You probably want:
Code:
sub pass {
my $char = shift
printf($char);
}
i.e. without the ($char) thingy.

Ivan
-----
Iyengar Yoga Resources / GT Plugins

Last edited by:

yogi: Dec 11, 2002, 11:47 AM
Quote Reply
Re: [Paul] Thanks for replying! In reply to
Code:
use Tk;
########################
# MainWindow Here: #
########################

my $main = MainWindow->new(
-background => 'blue',
);

$equals = $main -> Button(
-text => '=',
-justify => 'center',
-width => '2.5',
-height => '1.5',
-command => \&pass('1'),
-activebackground => 'skyblue',
) -> pack(
-expand => yes,
-fill => 'both',
);

$main -> title("Tk Calculator");

######################
# MainLoop Here: #
######################

MainLoop();

sub pass($char)
{
printf($char);
}

Last edited by:

Fat_N_Furry: Dec 11, 2002, 11:37 AM
Quote Reply
Re: [yogi] Thanks for replying! In reply to
In Reply To:
You probably want:
Code:
sub pass ($char)
{
printf($char);
}
i.e. without the ($char) thingy.

I believe I've tried both ways; I'll try that way again. BTW - you access that via the @_ array, right?
Quote Reply
Re: [Fat_N_Furry] Thanks for replying! In reply to
?
Quote Reply
Re: [Fat_N_Furry] Thanks for replying! In reply to
Yeah - I get the same result without the ($char).
Quote Reply
Re: [Paul] Thanks for replying! In reply to
In Reply To:
?

Whatya mean "?" ?
Quote Reply
Re: [Fat_N_Furry] Thanks for replying! In reply to
I mean why did you just paste the code in reply to my question.
Quote Reply
Re: [Paul] Thanks for replying! In reply to
You asked where MainWindow and MainLoop were defined - I just pointed them out for you. Smile

Last edited by:

Fat_N_Furry: Dec 12, 2002, 11:44 AM
Quote Reply
Re: [Fat_N_Furry] Thanks for replying! In reply to
They aren't defined - you call them but where are they implemented?...are they exported from Tk?

Last edited by:

Paul: Dec 12, 2002, 11:54 AM
Quote Reply
Re: [Paul] Thanks for replying! In reply to
Yeah - they're Tk things. MainWindow defines the window, MainLoop keeps the window open.
Quote Reply
Re: [Fat_N_Furry] Passing Variables in Perl/Tk??? In reply to
Hi, just found this thread when googling for Perl/Tk stuff.

-command => \&subroutine("variable_to_be_passed"),

You can't do it like that : it parses as

-command => \( &susubroutine("variable_to_be_passed") ),

i.e. it immediately calls subroutine, which returns 1 (the return value of the last command in the subroutine, print) and then the \ gets a reference to that 1, so you end up with

-command => \1,

What you really want is

-command => sub { subroutine("variable_to_be_passed") }

--

Utinni!
Quote Reply
Re: [utinni] Passing Variables in Perl/Tk??? In reply to
Thanks dude! I appreciate it. I have to go to church now, but I'll try it when I get home. Smile


Thankyou all!
Furry
Quote Reply
Re: [Fat_N_Furry] Passing Variables in Perl/Tk??? In reply to
Just wanted to let you know it worked like a charm. Cool