I`m writing simple xmpp client using Net::XMPP and Win32::GUI. I want to know how to make recieving loop assynchronous.

First I try to moved loop to another thread. But it fails with many errors:

Use of uninitialized value in concatenation (.) or string at C:/strawb +erry/perl/site/lib/XML/Stream.pm line 1439. Use of uninitialized value in hash element at C:/strawberry/perl/site/ +lib/XML/Stream.pm line 1440. Use of uninitialized value in concatenation (.) or string at C:/strawb +erry/perl/site/lib/XML/Stream.pm line 1440. Use of uninitialized value in hash element at C:/strawberry/perl/site/ +lib/XML/Stream.pm line 1443. Use of uninitialized value in numeric eq (==) at C:/strawberry/perl/si +te/lib/XML/Stream.pm line 1443. Use of uninitialized value in hash element at C:/strawberry/perl/site/ +lib/XML/Stream.pm line 1443. Use of uninitialized value within %status in numeric eq (==) at C:/str +awberry/perl/site/lib/XML/Stream.pm line 1506. Use of uninitialized value in subtraction (-) at C:/strawberry/perl/si +te/lib/XML/Stream.pm line 1507. Use of uninitialized value in concatenation (.) or string at C:/strawb +erry/perl/site/lib/XML/Stream.pm line 1669. Use of uninitialized value in numeric eq (==) at C:/strawberry/perl/si +te/lib/XML/Stream.pm line 1673. Use of uninitialized value in concatenation (.) or string at C:/strawb +erry/perl/site/lib/XML/Stream.pm line 1439. Use of uninitialized value in hash element at C:/strawberry/perl/site/ +lib/XML/Stream.pm line 1440. Use of uninitialized value in hash element at C:/strawberry/perl/site/ +lib/XML/Stream.pm line 1443. Use of uninitialized value in hash element at C:/strawberry/perl/site/ +lib/XML/Stream.pm line 1443. Use of uninitialized value in concatenation (.) or string at C:/strawb +erry/perl/site/lib/XML/Stream.pm line 1439. Use of uninitialized value in hash element at C:/strawberry/perl/site/ +lib/XML/Stream.pm line 1440. Use of uninitialized value in hash element at C:/strawberry/perl/site/ +lib/XML/Stream.pm line 1443. Use of uninitialized value in hash element at C:/strawberry/perl/site/ +lib/XML/Stream.pm line 1443. ....

So I use Win32::GUI::Timer, which call $client->Process(1) every 3 seconds. But function need some time to execute, so window freezes for a moment. How can I avoid these and call Process() asynchronously?

use Win32::GUI(); use Net::XMPP; use feature 'say'; my $wnd = Win32::GUI::Window->new( -size => [320, 240] ); $wnd->AddTextfield( -name => 'messages', -size => [$wnd->ScaleWidth, 130], -pos => [0, 0], -multiline => 1 ); $wnd->AddTextfield( -name => 'new_message', -size => [$wnd->ScaleWidth, 20], -pos => [0, 140], -multiline => 1 ); $wnd->AddButton( -text => 'Send', -size => [80, 24], -pos => [$wnd->ScaleWidth / 2 - 40, 170], -onClick => \&send_message ); $wnd->Center; $wnd->Show; my $client = new Net::XMPP::Client; $client->SetCallBacks( message => sub { my ($mid, $message) = @_; say 'message recieved'; $wnd->messages->Text($wnd->messages->Text() . '[' . $message-> +GetFrom('jid')->GetUserID . '] ' . $message->GetBody . "\r\n") } ); my $result = $client->Connect( hostname => 'xmpphostname', port => '5222', timeout => 7, ssl => 0, tsl => 0 ); if ($result) { $client->AuthSend( username => 'user1', password => '123' ); } if ($client->Connected) { say 'connected to server'; } else { say 'error connecting to server: ' . $!; exit 0; } my $pres = Net::XMPP::Presence->new(); $pres->SetType('available'); $client->Send($pres); my $timer = $wnd->AddTimer('timer', 3000); Win32::GUI::Dialog(); sub timer_Timer() { if ($client->Connected) { $client->Connect() unless defined $client->Process(1); } else { $client->Connect() or die('cant connect'); } } sub send_message() { my $msg = $wnd->new_message->Text(); $client->MessageSend( to => 'user2@xmpphostname', type => 'chat', body => $msg ); $wnd->messages->Text($wnd->messages->Text() . "[me] $msg\r\n"); $wnd->new_message->Text(''); }

In reply to XMPP client with Win32::GUI by artem78

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.