Cybris has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks,

I'm trying to repair an old script that uses Class::Util (from the old OOTools package) so that it works with newer Perl versions. Unfortunately, I'm running into a bug because some perl behaviour has changed in 5.10 (https://rt.cpan.org/Public/Bug/Display.html?id=29824))

Now, the problem seems to be that that technique of checking whether a package exists no longer works in 5.10 and above. So, my question is: how do you check if any arbitrary package exists? Without knowing the names of any functions in that package?

Unfortunately my legacy script is too big to rewrite (of course, I didn't even write it in the first place, but "inherited" it).

Your help would be very appreciated.

Replies are listed 'Best First'.
Re: Check if a package exists
by Corion (Patriarch) on Sep 11, 2010 at 22:03 UTC

    Instead of checking for

    defined %{ 'some::package::' }

    ... which has been deprecated for a long time and subsequently been removed, why not check whether there are any elements within the package?

    perl -wle "package foo; sub unknown {}; package main; print for keys % +{ 'foo::' }"

    In other words, keys %{ 'some::package' } should tell you whether a package exists, or rather, whether it contains any things of note (to Perl).

    Update: perl56delta tells us:

    defined(%hash) is deprecated

    (D) defined() is not usually useful on hashes because it checks for an undefined scalar value. If you want to see if the hash is empty, just use if (%hash) { # not empty } for example.

Re: Check if a package exists
by BrowserUk (Patriarch) on Sep 11, 2010 at 23:09 UTC

    Rather than defined, use:

    print scalar %{ List::Util:: };; 18/32 print scalar %{ Non::Existant:: };; 0

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Check if a package exists
by Khen1950fx (Canon) on Sep 12, 2010 at 03:49 UTC
    A slightly longer way...This worked for me on 5.10.1.
    #!/usr/bin/perl use strict; use warnings; use Class::Util; sub dsym { my($hashRef) = shift; my(%symbols); my(@symbols); %symbols = %{$hashRef}; @symbols = sort(keys(%symbols)); foreach (@symbols) { printf("%-10.10s| %s\n", $_, $symbols{$_}); } } print dsym(\%Class::Util::);
Re: Check if a package exists
by Anonymous Monk on Sep 12, 2010 at 03:53 UTC
    do you mean something like this?
    $ perl -MData::Dumper -e1 $ echo $? 0 $ perl -MImNot::Installed -e1 .... ImNot::Installed not found in @INC ... $ echo $? 1
Re: Check if a package exists
by JavaFan (Canon) on Sep 13, 2010 at 21:42 UTC
    So, my question is: how do you check if any arbitrary package exists?
    That depends on what you mean by "a package exists". From a language point of view, it's not a question that makes a lot of sense. All packages are equal.