On Tue, Dec 07, 2004 at 10:45:16PM -0000, kai @ bolay. de wrote:
> expected to be able to use the FileCache module from any package
> module I want. Obviously it doesn't work (see below). I'm not enough
> of an expert with typeglobs and package scoping to fix this myself.
> 
> As a workaround I temporarily switch to the "main" package when using
> FileCache. That works but seems like a bad hack.

Unless I'm misunderstanding, this is not a bug in FileCache.  This is how 
most modules which export functions work.

   use Text::Soundex;  
   print soundex("foo");  

   package Foo;  
   print soundex("bar")'

F000
Undefined subroutine &Foo::soundex called at -e line 1.

Module will typically only export their functions into the current package.
This is to prevent "namespace pollution", modules and functions in one
package showing up in another.  This is basic encapsulation.

If you want to use FileCache or any module's functions in a package you
must first use that module *in that package*.  It is fine to use a module
more than once in the same program.


> eval {
>     package anything;

Add a "use File::Cache;" here and you're good.

>     no strict "refs";
>     my $filename = "testfile.$$.tmp";
>     cacheout($filename);
>     print $filename "data\n";
>     unlink($filename);
> };
> 
> if (!$@) {
>     print "using FileCache in any package works\n";
> } else {
>     print "using FileCache in any package fails: $@";
> }

-- 
Michael G Schwern        [EMAIL PROTECTED]  http://www.pobox.com/~schwern/
What's a classy place like this doing around a low-life like you?

Reply via email to