export pdl() only from perl PDL

185 views Asked by At

I want to export only the pdl() function from PDL to avoid namespace collision with other automatically exported functions such as max, median, sum, intersect etc. However, when I tried

use PDL qw(pdl);

it doesn't work as the aforementioned functions still get exported (and I get warnings on functions being redefined).

On the other hand, if I do

use PDL qw();

The collision doesn't happen. But when I try to use the pdl() function in the program via PDL::pdl(), it fails with this error message:

Can't call method "new" on unblessed reference at Basic/Core/Core.pm.PL (i.e. PDL::Core.pm) line 934.

Also, I tried inside the script

{
     require PDL;
     pdl();
}

I get the same error message.

Can someone advice on how I can export pdl() only? Thx!

2

There are 2 answers

1
Cornel Ghiban On BEST ANSWER

SYNOPSIS use PDL; # Is equivalent to the following:

   use PDL::Core;
   use PDL::Ops;
   use PDL::Primitive;
   use PDL::Ufunc;
   use PDL::Basic;
   use PDL::Slices;
   use PDL::Bad;
   use PDL::MatrixOps;
   use PDL::Math;
   use PDL::Version;
   use PDL::IO::Misc;
   use PDL::IO::FITS;
   use PDL::IO::Pic;
   use PDL::Lvalue;

I guess if you only import PDL::Core, you'll get only the functions in this module.

0
David Mertens On

I'm actually a bit surprised that use PDL qw() does anything different from use PDL. In fact, I don't see how it can: the code from PDL::import doesn't pay attention to its arguments. A different module was created for this very purpose called PDL::Lite, which is equivalent to:

use PDL::Core '';
use PDL::Ops '';
use PDL::Primitive '';
use PDL::Ufunc '';
use PDL::Basic '';
use PDL::Slices '';
use PDL::Bad '';
use PDL::Version;
use PDL::Lvalue;

Some of the extras from the usual use PDL are cut out, and none of the functions are imported. (PDL::Lvalue and PDL::Version do not export anything.)

On to your second question, when you use PDL::Lite, you can still get at the pdl constructor with PDL->pdl(1, 2, 3). You cannot say PDL::pdl(1, 2, 3) because the constructor is actually defined in the PDL::Core package. Go figure. So, PDL::Core::pdl(1, 2, 3) does what you meant, but is not the obvious choice.