in reply to How to check if a function exists within a package?

To check if something exists use exists. ;)

use strict; use warnings; require ('Encode.pm'); if (exists &Encode::encode) { print "Exists\n"; } else { print "None such\n"; }

Prints:

Exists

Perl reduces RSI - it saves typing

Replies are listed 'Best First'.
Re^2: How to check if a function exists within a package?
by ikegami (Patriarch) on Sep 08, 2008 at 01:29 UTC

    defined vs exists:

    sub foo {} sub bar; my @names = qw( foo bar baz ); local $, = "\t"; local $\ = "\n"; print('', @names); print('exists', map { exists(&$_) ?1:0 } @names); print('defined', map { defined(&$_) ?1:0 } @names);
    foo bar baz exists 1 1 0 defined 1 0 0

    You can't call the ones that exist but aren't defined, so defined is probably a better choice.

    >perl -e"sub bar; bar()" Undefined subroutine &main::bar called at -e line 1.