Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.

So, I have an array which contains packages names. Those packages should have a sub 'ReceiveMessage'.
A package called Communications contains a sub 'SendMessage'.
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".

package Communications; sub SendMessage { my $msg = join("", @_); foreach my $cpack (@main::plugpack) { if (defined eval "${cpack}::ReceiveMessage") { eval "\&${cpack}::ReceiveMessage('$msg')"; } } } sub ReceiveMessage { print "Communications::ReceiveMessage('$_[0]')\n"; }
Of course I call the SendMessage sub once: &Communications::SendMessage("Hello, World!");
The strange thing is, I get output like I called SendMessage twice:
Use of uninitialized value in concatenation (.) or string at D:\device +\modem\internet\Xitami\cgi-bin\ line 48. Communications::ReceiveMessage('') Communications::ReceiveMessage('Hello, World!')
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?

Update: just clarified the last paragraph a little
Update: removed line of code which was coded in a inconsistent style and only was there for debug purposes :)

In reply to See if a certain package has a certain sub, without executing that sub by muba

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    [marto]: good morning all
    [Corion]: Hi marto!
    [choroba]: Good morning!
    [Corion]: I hope you had a good weekend!
    [marto]: jetlag has really done a number on the kids, it's been a tough week
    choroba played with the band on Saturday, so Sunday was very sleepy
    [choroba]: managed to release Syntax::Construct with 5.28 support in the night, though
    [Corion]: choroba: Whee ;)
    [Corion]: marto: Ouch - I would've thought that kids adapt much better, but that's obviously not the case...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (10)
    As of 2018-06-25 08:35 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (126 votes). Check out past polls.