Friday, March 8, 2013

How could you check the existence of a function (say new) within a package (say CGI) in Perl?

use CGI;

$cgi=CGI->new;

if (exists &CGI::new)
{
    print "Exists\n";
}

Or

if ( defined( &CGI::new ) )
{
print "Defined\n";
}

Or

if ( CGI->can('new') )
{
print "Exists\n";
}

Or

if ( $cgi->can('new') )
{
print "Defined\n";
}

No comments :