What does this line mean in perl?

I am looking at the code, but I do not understand what the following line does:

return if !%Foo::Details:: ; 

What exactly does this do? Does it check for the presence of the Foo :: Details module?

+6
source share
2 answers

A hash in a scalar context returns false if it is empty, so your code returns an empty list if the hash %Foo::Details:: empty.

This hash is a character table for the Foo::Details namespace. If a batch variable or subfolder is created in the Foo::Details namespace, a globe will be created in %Foo::Details:: that matches the name of the variable or sub. Thus, it returns an empty list if the Foo :: Details namespace is empty.

 $ cat >Foo/Details.pm package Foo::Details; sub boo { } 1; $ perl -E'say %Foo::Details:: ?1:0;' 0 $ perl -E'use Foo::Details; say %Foo::Details:: ?1:0;' 1 

You might be trying to check if the Foo :: Details module is loaded, but it is not perfect. For example, he will think that Foo :: Details is loaded, even if only Foo :: Details :: Bar is loaded. To check if Foo :: Details has been loaded, it is better to check if $INC{"Foo/Details.pm"} . The problem with this approach is that it will not find "built-in modules."

+9
source

This code checks if the module is loaded. %Foo::Details:: - the symbol table for the Foo::Details module.

If Foo::Details has never loaded, %Foo::Details:: returns false (because it contains no elements), which if negated with ! returns true and returns from any function you are in.

You can read more about symbol tables in perlmod#Symbol Tables

In addition, here is another SO post , which talks about ways to determine module loading.

+3
source

All Articles