<?xml version="1.0" encoding="windows-1252"?>
<node id="338296" title="See if a certain package has a certain sub, without executing that sub" created="2004-03-20 10:03:42" updated="2005-03-27 09:31:30">
<type id="115">
perlquestion</type>
<author id="333489">
muba</author>
<data>
<field name="doctext">
Hi,&lt;BR&gt;&lt;BR&gt;

I am writing a simple plugin system. I'm not sure whether I'm going to use it, but it's more like a way to explore Perl.&lt;BR&gt;&lt;BR&gt;
So, I have an array which contains packages names. Those packages should have a sub 'ReceiveMessage'.&lt;BR&gt;A package called Communications contains a sub 'SendMessage'.&lt;BR&gt;This SendMessage loops over the array to check which packages do have a ReceiveMessage and send a message to it. Currently, the array consists of one item: "Communications".&lt;BR&gt;&lt;BR&gt;

&lt;CODE&gt;
package Communications;

sub SendMessage {
    my $msg = join("", @_);
    foreach my $cpack (@main::plugpack) {
        if (defined eval "${cpack}::ReceiveMessage") {
            eval "\&amp;${cpack}::ReceiveMessage('$msg')";
        }
    }
}

sub ReceiveMessage {
    print "Communications::ReceiveMessage('$_[0]')\n";
}
&lt;/CODE&gt;

Of course I call the SendMessage sub once: &lt;CODE&gt;&amp;Communications::SendMessage("Hello, World!");&lt;/CODE&gt;&lt;BR&gt;
The strange thing is, I get output like I called SendMessage twice:
&lt;code&gt;
Use of uninitialized value in concatenation (.) or string at D:\device\modem\internet\Xitami\cgi-bin\index.pl line 48.
Communications::ReceiveMessage('')
Communications::ReceiveMessage('Hello, World!')
&lt;/code&gt;

It appears to me that ReceiveMessage is not only called when the message is sent, but also when SendMessage checks it's existance. How can I prevent this?&lt;BR&gt;&lt;BR&gt;

&lt;B&gt;Update:&lt;/B&gt; just clarified the last paragraph a little&lt;BR&gt;
&lt;B&gt;Update:&lt;/B&gt; removed line of code which was coded in a inconsistent style and only was there for debug purposes :)</field>
</data>
</node>
