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

hello monks, i have a problem with ptk.
here is part of the code:
$nameEntry->bind('<Return>', sub { printx($sock,"PRIVMSG #testsen :$sn +dtxt"); });
bind works great, but the problem is accessing the sub routine, i cannot access it!
here is the error:

Tk::Error: Can't use an undefined value as symbol reference at bot.pl line 312.
(Command bound to event)

here is the lines:

310 sub printx { 311 my $sock = shift; 312 print $sock "@_\n"; 313 }
any suggestions???

- ruben

Replies are listed 'Best First'.
Re: Perl/TK Error
by pg (Canon) on Apr 12, 2003 at 04:53 UTC
    Made up this demo, which partly used your code. I tested and it worked. To use just enter something like www.perlmonks.com

    Compare this with yours and see what's the difference.
    use Tk; use IO::Socket::INET; use strict; my $mw = new MainWindow; my $sndtxt = "www.google.com"; my $nameEntry = $mw->Entry(width => 80, textvariable => \$sndtxt)->pac +k; $nameEntry->bind('<Return>', sub {my $sock = new IO::Socket::INET(Prot +o=> "tcp", PeerAddr=> $sndtxt, PeerPort => "80") || die "failed"; #chomp $sndtxt; printx($sock, "GET http://$sndtxt/ H +TTP/1.1\r\nHost: $sndtxt\r\n\r\n"); $sock->close();} ); my $text = $mw->Scrolled("Text", width => 80, height => 30, -scrollbar +s => "se")->pack; MainLoop; sub printx { my $sock = shift; print $sock shift; $text->delete("1.0", "end"); while (<$sock>) { print $_; $text->insert("end", $_); } }
Re: Perl/TK Error
by The Mad Hatter (Priest) on Apr 12, 2003 at 04:27 UTC
    Doing a little experimentation, it seems as though that error is occuring because $sock isn't a valid filehandle to print to. Make sure $sock is what you think it is.

    Could you tell us more about $sock and where it comes from (ex/ what's the open call look like)?

Re: Perl/TK Error
by Necos (Friar) on Apr 12, 2003 at 16:02 UTC
    I'd also suggest looking at your bind. The '#testsen' part might be what's causing your problem. You might want to enclose it in single quotes so perl doesn't try to read it as a comment.

    Just some food for thought.

    Theodore Charles III
    Network Administrator
    Los Angeles Senior High
    email->secon_kun@hotmail.com
    perl -e "map{print++$_}split//,Mdbnr;"
      is not that, i fixed it :)
      btw thanx pg for that example!

      now can someone please help me with forking?
      im tryng to maintain a socket connection alive,
      and at the same time i want to use a perl TK gui,
      from that gui i want to be able to send stuff to the socket connection while is alive
      do i need to fork? or what can i do?

      thank you perl monks!