<?xml version="1.0" encoding="windows-1252"?>
<node id="338300" title="Re: See if a certain package has a certain sub, without executing that sub" created="2004-03-20 10:27:14" updated="2005-06-08 10:42:47">
<type id="11">
note</type>
<author id="127116">
gmax</author>
<data>
<field name="doctext">
&lt;p&gt;Use the &lt;u&gt;can&lt;/u&gt; method (it's a method of the [UNIVERSAL ]package, so every class in Perl has access to it).&lt;/p&gt;

&lt;code&gt;
#!/usr/bin/perl

use strict;
use warnings;

package Communications;

sub SendMessage {
    my $packages = shift;
    my $msg = shift;
    for (@$packages) {
        if ($_-&gt;can('ReceiveMessage') ) {
            $_-&gt;ReceiveMessage($msg);
        }
        else {
            print "$_ can't ReceiveMessage\n"
        }
    }
}

sub ReceiveMessage {
    print __PACKAGE__, "::ReceiveMessage('$_[1]')\n";
}

package Second;

sub ReceiveMessage {
    print __PACKAGE__, "::ReceiveMessage('$_[1]')\n";
}

package Other;

sub nothing {
    print "\n";
}

package main;

my @packages = qw(Communications Other Second);

Communications::SendMessage( \@packages, 'hello');
__END__
output:
Communications::ReceiveMessage('hello')
Other can't ReceiveMessage
Second::ReceiveMessage('hello')
&lt;/code&gt;


&lt;div class="pmsig"&gt;
&lt;div class="pmsig-127116"&gt;
&lt;pre&gt;
 _  _ _  _  
(_|| | |(_|&gt;&lt;
 _|   
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;</field>
<field name="root_node">
338296</field>
<field name="parent_node">
338296</field>
</data>
</node>
