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

I am getting an error when using the perl index function when my perl script contains the "use PDL" statement.

Example code:

#!/usr/bin/perl -w use strict; use PDL; use PDL::IO::FITS; my $test = "This is a test and I hope it works."; my $pos4 = index($test,"test"); print "pos4 = $pos4\n"; exit(0);

This gives me the error:

Argument "This is a test and I hope it works." isn't numeric in subroutine entry at index_test.pl line 6.

Argument "test" isn't numeric in subroutine entry at index_test.pl line 6.

pos4 = 0

There is no error if I comment out the "use PDL;" statements; and $pos4 contains the correct character position for the substring "test". My perl script needs to work with FITS files, so I need the PDL module.

Does anyone have a suggestion on how I can get this to work?

Replies are listed 'Best First'.
Re: perl index function error when "use PDL"
by pvaldes (Chaplain) on Oct 27, 2011 at 23:32 UTC
    It seems that the problem is that you're implicitly loading PDL::index here instead the perl function index. Don't load the PDL::index unless you think that you will need it
    #!/usr/bin/perl -w use strict; use PDL::Core; use PDL::Index; use PDL::Basic; use PDL::Math; use PDL::Primitive; use PDL::Slices qw/!index/; # <-HERE is the problem (and the solution) use PDL::IO::FITS; my $test = "This is a test and I hope it works."; my $pos4 = index $test,"test"; print "pos4 = $pos4\n"; exit(0); __END__ pos4 = 10

    Updated: In fact it was a little more complicated, really PDL::Index was not the culprit at the end, they are three different index here:

    the perl function index a PDL function index in the module PDL::Slices (the real culprit) and a module named PDL::Index
      Yes, it's the index function being exported by PDL::Slices that causes the problem.
      Why does perl fail to warn that index has been re-defined ?

      Cheers,
      Rob
        Why does perl fail to warn that index has been re-defined ?

        Because its not illegal to redefine built-ins. Just silly unless they perform the same function.

        C:\test>perl -Mstrict -wE"sub map{ 1 }"

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        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: perl index function error when "use PDL"
by Lotus1 (Vicar) on Oct 28, 2011 at 01:56 UTC
    my $pos4 = CORE::index($test,"test");
Re: perl index function error when "use PDL"
by Khen1950fx (Canon) on Oct 28, 2011 at 01:55 UTC
    Use PDL::Core instead of PDL. Also, you haven't used a pdl index function, so the perl builtin works fine here.
Re: perl index function error when "use PDL"
by zentara (Cardinal) on Oct 28, 2011 at 15:16 UTC
    Hi, not to hijack your thread, but I just ran into a similar glitch with PDL's overriding core subroutines.

    It all involved the claim, that what you entered in the PDL shell, perldl, would also run as a Perl shell script. So I took some code from the Newbook tutorial, ran it fine in perldl, then copy&pasted it to a Perl shell script. I got this weird error:

    " syntax error at ./z line 18, near "$gal("
    Global symbol "$section" requires explicit package name at ./z line 19, <DATA> line 387.
    Execution of ./z aborted due to compilation errors."
    
    the code is this
    #!/usr/bin/perl use warnings; use strict; use PDL; use PDL::Graphics::PGPLOT; my $a = rfits "m51_raw.fits"; my $flat = rfits "m51_flatfield.fits"; imag $flat; <>; my $gal = $a / $flat; # !!!!!!!!! this is line 18 !!!!!!!!!!!!!!!!!!!!!! my $section = $gal(337:357, 178:198 ); imag $section; # It's the bright star <>; __END__

    It's an overriden function, and I forgot perldl says in it's statrup text that PDL::NiceSlice; is autoloaded with Readline, and are overriden. Giving no error other than a syntax error.

    Interestingly, in the module source dir, PDL/utils is a script named perldlpp.pl, whose job it is to filter out the NiceSlice overrides, and change it to legal core Perl. After running my script thu this filter, line 18 changes like this, and the script runs fine without using "use PDL::NiceSlice;"

    # pre:filter line 18 is this: # my $section = $gal(337:357, 178:198 ); # imag $section; # It's the bright star # post filter line 18 is this, and runs without "use PDL::Nice::Slice" my $section = $gal->nslice([337,357,1],[178,198,1]); imag $section; # It's the bright star

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: perl index function error when "use PDL"
by allori (Initiate) on Oct 28, 2011 at 15:19 UTC

    Thanks to all Monks who offered suggestions to solve my problem.

    I appreciated especially Lotus1's example code.

    I just replaced  my $pos4 = index($test,"test"); with  my $pos4 = CORE::index($test,"test"); and everything is fine now.

    I thought that my problem was related to perl trying to use some piddle index method, but was not aware of using CORE to refer back to a perl core function. Thanks again to all!