how to use callback function

B

bingster

I'm stuck.
http://search.cpan.org/~reatmon/Net-XMPP-1.0/lib/Net/XMPP/Message.pm is
the manual page for the Net::XMPP::Message module.

What I need to do is to retrieve the 'from' part from the following message:

<message type='groupchat' to='(e-mail address removed)'
from='(e-mail address removed)'><body>foo has left: Logged out</body><x
xmlns='jabber:x:delay' from='(e-mail address removed)'
stamp='20050119T13:27:58'/></message>

The DESCRIPTION part of the module manual shows:

============
use Net::XMPP;

sub message {
my ($sid,$Mess) = @_;
 
B

Brian McCauley

bingster said:
Actually, my question should be how to pass an object to a sub-routine?

Er, just do it. Where do you percive that there is any difficulty?

Suppose there is an object (well actually a reference to a blessed
thingy) in $foo. And you want to pass that to the subrouine bar():

bar($foo);
 
A

Anno Siegel

bingster said:
Actually, my question should be how to pass an object to a sub-routine?

No, that's not your question. The sub message() is a callback routine.
It is called for you, and any object-passing is done by your friendly
module.

You don't. The nature of a callback is that it is called for you. What
*you* do is write the subroutine message() accordingly (untested):

sub message {
my ( $id, $msg) = @_
my $from = $msg->from;
# now do with $from what you want to do
}

At least that's how I read the documentation you quote.

Anno
 
B

bingster

Anno said:
No, that's not your question. The sub message() is a callback routine.
It is called for you, and any object-passing is done by your friendly
module.




You don't. The nature of a callback is that it is called for you. What
*you* do is write the subroutine message() accordingly (untested):

sub message {
my ( $id, $msg) = @_
my $from = $msg->from;
# now do with $from what you want to do
}

At least that's how I read the documentation you quote.

Anno

Bare with me. But I still don't understand how callback function should
work. How is the 'message' sub-routine called? This is what I've tried.

========
#!/usr/bin/perl

use Net::XMPP;
use Net::XMPP::Message;

$mess = "<message type='groupchat' to='(e-mail address removed)'
from='(e-mail address removed)'><body>foo has left: Logged out</body><x
xmlns='jabber:x:delay' from='(e-mail address removed)'
stamp='20050119T13:27:58'/></message>";

&message(0,$mess);

sub message {
my ($sid,$Mess) = @_;
$from = $Mess->GetFrom();
print "to is $to\n";
}
=========

The 'GetFrom()' is a method definded in the Net::XMPP::Message module.

It returned Can't call method "GetFrom" without a package or object
reference at ./test.pl line 12.

Should I do '$mess = new Net::XMPP::Message()' to create an object in
the main script? How should I pass that '<message....></message>'
string to the $mess object?

Thanks a lot for the help,

Bing
 
X

xhoster

bingster said:
Bare with me. But I still don't understand how callback function should
work. How is the 'message' sub-routine called? This is what I've tried.

If message is a call-back, then presumable you don't call it--the module
does. Usually, that means you would at point take a ref to the sub and
pass that ref into a constructor or something. The documenation doesn't
seem very good one this point, but it does say "There is are example
scripts in the example directory that provide you with examples of very
simple XMPP programs."

Looking at those example programs should clear things up.

Xho
 
A

Anno Siegel

bingster said:
Bare with me.

Thanks, but no :)
But I still don't understand how callback function should
work. How is the 'message' sub-routine called? This is what I've tried.

========
#!/usr/bin/perl

use Net::XMPP;
use Net::XMPP::Message;

$mess = "<message type='groupchat' to='(e-mail address removed)'
from='(e-mail address removed)'><body>foo has left: Logged out</body><x
xmlns='jabber:x:delay' from='(e-mail address removed)'
stamp='20050119T13:27:58'/></message>";

&message(0,$mess);

$mess is a plain string here, not a Net::XMPP::Message object, as
required.
sub message {
my ($sid,$Mess) = @_;
$from = $Mess->GetFrom();
print "to is $to\n";
}
=========

The 'GetFrom()' is a method definded in the Net::XMPP::Message module.

It returned Can't call method "GetFrom" without a package or object
reference at ./test.pl line 12.

Sure. You didn't pass an object in.
Should I do '$mess = new Net::XMPP::Message()' to create an object in
the main script? How should I pass that '<message....></message>'
string to the $mess object?

I don't know. See if there's a documented creator for Net::XMPP::Message
objects. If the documentation doesn't mention one, there is none. If
the module is any good, that means that you don't need one in normal
operation. Otherwise, proceed as documented.
Thanks a lot for the help,

I can't give you any more specific help -- I don't know Net::XMPP, not
to mention XMPP itself.

Unspecifically, I have the impression you got something backwards.

The message() callback would be called (I'm speculating a bit here)
when a message *arrives*, whatever that means in XMPP. Then you get a
ready-made message object in $Mess and can call the methods. But that
would be a message that comes from somewhere else. You don't (normally)
get to set it up in the same part of the program that receives it.

So, either you have to arrange for the specific message you want to
analyze to arrive and trigger a callback.

Or forget about the callback and see if you can create a message object
directly. Then you can apply methods like ->GetFrom to that object
without the help of message(). Unless this is for experimentation,
it looks a bit futile -- presumably you know what you have put in the
"From" slot when you created the object.

Anno
 
B

bingster

If message is a call-back, then presumable you don't call it--the module
does. Usually, that means you would at point take a ref to the sub and
pass that ref into a constructor or something. The documenation doesn't
seem very good one this point, but it does say "There is are example
scripts in the example directory that provide you with examples of very
simple XMPP programs."

Looking at those example programs should clear things up.

Xho
Speaking of the example programs, if I had had access to any example
code, I would not have come here to bother you guys. I saw the manual
mentioned the example directory as well. However I've searched my
system from root, but did not find any related example code. I e-mailed
the author of the module. But got no response. Also searching
Net::XMPP in this newsgroup returns nothing but my posts. Anyway,
thanks a lot for looking.

Bing
 
B

bingster

Anno Siegel wrote:

The message() callback would be called (I'm speculating a bit here)
when a message *arrives*, whatever that means in XMPP. Then you get a
ready-made message object in $Mess and can call the methods. But that
would be a message that comes from somewhere else. You don't (normally)
get to set it up in the same part of the program that receives it.

That makes some sense.
So, either you have to arrange for the specific message you want to
analyze to arrive and trigger a callback.

Dunno how to do it at this point.
Or forget about the callback and see if you can create a message object
directly. Then you can apply methods like ->GetFrom to that object
without the help of message(). Unless this is for experimentation,
it looks a bit futile -- presumably you know what you have put in the
"From" slot when you created the object.

This is something I want to try, that is, forget about the callback and
create a message object directly. Any quick pointers that can show me
how to create a message object directly?

THANKS!

Bing
 
S

Sherm Pendley

bingster said:
Speaking of the example programs, if I had had access to any example
code, I would not have come here to bother you guys. I saw the manual
mentioned the example directory as well. However I've searched my
system from root, but did not find any related example code.

The 'examples/' subdirectory is not installed on your system, it's part of
the module source. Just go to CPAN and download the module tarball and
unpack it. Or use 'look' in the CPAN shell to do the same thing.

I didn't look very closely at the example, but client.pl looks promising -
there's a call to SetCallbacks(), and implementations of callback methods.

sherm--
 
A

A. Sinan Unur

bingster said:
Speaking of the example programs, if I had had access to any example
code, I would not have come here to bother you guys. I saw the manual
mentioned the example directory as well. However I've searched my
system from root, but did not find any related example code. I e-mailed
the author of the module. But got no response. Also searching
Net::XMPP in this newsgroup returns nothing but my posts. Anyway,
thanks a lot for looking.

Oh, c'mon ...

http://search.cpan.org/src/REATMON/Net-XMPP-1.0/examples/

The steps it took to find it:

1. http://search.cpan.org/

2. Type xmpp in the search box

3. Follow < Net::XMPP >

4. Follow < Net-XMPP-1.0 >

5. Follow < Browse >

6. Look in the examples directory

I am not sure which one of these steps is not obvious.

Sinan
 
A

Anno Siegel

bingster said:
Anno Siegel wrote:
[...]
Or forget about the callback and see if you can create a message object
directly. Then you can apply methods like ->GetFrom to that object
without the help of message(). Unless this is for experimentation,
it looks a bit futile -- presumably you know what you have put in the
"From" slot when you created the object.

This is something I want to try, that is, forget about the callback and

Seems reasonable.
create a message object directly. Any quick pointers that can show me
how to create a message object directly?

No. At this point, that would mean I'd read the documentation for you.
If you have specific questions about parts of the doc, you're welcome
back.

Anno
 
B

bingster

Sherm said:
The 'examples/' subdirectory is not installed on your system, it's part of
the module source. Just go to CPAN and download the module tarball and
unpack it. Or use 'look' in the CPAN shell to do the same thing.

Thanks a bunch, Sherm, for jumping in at the right time! I did not know
examples/ was not installed. Following what you said, I'm able to see
the example programs now. That's a really big help.
I didn't look very closely at the example, but client.pl looks promising -
there's a call to SetCallbacks(), and implementations of callback methods.

sherm--

Yup. It absolutely let me see some light at the end of the tunnel.

Bing
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,166
Messages
2,570,907
Members
47,446
Latest member
Pycoder

Latest Threads

Top