2

I have found this:

my $rc = eval
{
  require Term::ReadKey;
  Term::ReadKey->import();
  1;
};

if($rc)
{
  # Term::ReadKey loaded and imported successfully
  ...
}

But that does not work for packages defined inside other modules like:

{
  package Hi::Test;
}    

my $rc =  eval{ require Hi::Test };

$rc is false here.

How can I check that 'Hi::Test' is available?

2

4 Answers 4

3

I'm assuming there is actually something happening in that package, and not just an empty block.

The following code checks if there are any entries in the symbol table for that package. It's dirty, but it works as long as there are subs or package variables registered.

{
  package Hi::Test;

  sub foo;
}

my $rc =  eval{ require Hi::Test };
if (! $rc) {
  $rc = do {
    no strict;
    *stash = *{"Hi::Test::"};
    scalar keys %stash;
  }
}

print $rc;

It will print 1.

1
  • 1
    defined(*{"Hi::Test::"}) is sufficient.
    – ikegami
    Sep 16, 2015 at 17:44
2

You want something like defined(*Hi::Test::), except that simply mentioning *Hi::Test:: creates the package.

$ perl -E'
   say defined(*Hi::Test::) ? "exists" : "doesn'\''t exist";
'
exists

By using symbolic references, you avoid that problem.

$ perl -E'
   { package Hi::Test }
   say defined(*{"Hi::Test::"}) ? "exists" : "doesn'\''t exist";
   say defined(*{"Hi::TEST::"}) ? "exists" : "doesn'\''t exist";
'
exists
doesn't exist

Putting that code in a sub to makes things cleaner.

$ perl -E'
   use strict;
   use warnings;

   sub test_for_package {
      my ($pkg_name) = @_;
      $pkg_name .= "::";
      return defined(*$pkg_name);
   }

   { package Hi::Test }
   say test_for_package("Hi::Test") ? "exists" : "doesn'\''t exist";
   say test_for_package("Hi::TEST") ? "exists" : "doesn'\''t exist";
'
exists
doesn't exist

Note that creating the package Foo::Bar::Baz also creates the packages Foo and Foo::Bar.

5
  • There seems a bug in perl when eval 'require Some::Pkg' which spoil global namespace. rt.perl.org/Ticket/Display.html?id=126077 Sep 17, 2015 at 6:35
  • @Eugen Konkov, Yes, as mentioned, referencing Some::Pkg creates the package, so require Some::Pkg creates the package, even if it eventually throws an exception.
    – ikegami
    Sep 17, 2015 at 15:48
  • @Eugen Konkov, Interesting. no strict qw( refs ); is indeed not required. Fixed.
    – ikegami
    Sep 17, 2015 at 15:48
  • I think that is due to 'defined' which does not produce error Sep 17, 2015 at 16:21
  • It does seem to be. That's very interesting once you realize that defined is only called after the strict error would have been thrown. That means they went out of their way to support this by treating it specially in the compiler.
    – ikegami
    Sep 17, 2015 at 16:39
1

I'm a little rusty on this, but I think your require will be failing regardless - this errors:

#!/usr/bin/perl

{
    package Hi::Test;

    sub foo {
        print "bar\n";
    }
}

{
    package main;
    require Hi::Test; 
}

This errors - it can't find it @INC (because it isn't in @INC). Both use and require specifically tell perl to "go out and find a module file"

But you can still call 'foo' with:

Hi::Test::foo();

So you can't test the loading of the module with eval nor can you check %INC .

But what you can do is check %Hi:::

use Data::Dumper;
print Dumper \%Hi::;
print Dumper \%Hi::Test::;

Which gives us:

$VAR1 = {
          'Test::' => *{'Hi::Test::'}
        };
$VAR1 = {
          'foo' => *Hi::Test::foo
        };

So we can:

print "Is loaded" if defined $Hi::{'Test::'}
0

UPDATED
I have found this clue:

my $module =  *main::;
my @sub_name =  split '::', $full_name;
while( each @sub_name ) {
    $module =  $$module{ $sub_name[$_].'::' };
}
print "Module is available"   if $module;

In compare to this answer it does not create additional variable in global stash

3
  • Please attribute where you have found that.
    – simbabque
    Sep 16, 2015 at 13:49
  • this can be found in perlmod - each package has a magic hash called %packagename:: So you can do print Dumper \%main:: - or in this example: print Dumper \$main::{'Hi::'}; giving $VAR1 = \*{'::Hi::'};. What you can't do is: print Dumper \$main::{'Hi::Test::'};
    – Sobrique
    Sep 16, 2015 at 13:54
  • %::, %main::, %main::main:: and %main::main::main:: are all the same namespace. I'd drop the main from your code.
    – ikegami
    Sep 16, 2015 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.