Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

perlfunc index name conflict

by dpath2o (Acolyte)
on Sep 23, 2012 at 22:13 UTC ( #995263=perlquestion: print w/ replies, xml ) Need Help??
dpath2o has asked for the wisdom of the Perl Monks concerning the following question:

Hi, This is somewhat basic so I apologise if this has been answered somewhere, but I had difficulty in finding the solution using some simple searches. Regardless, my problem is that I'm having a conflict in using 'PDL::index' and Perl's 'index'. Basically I have a script that is using predominantly PDL coding but I'd like to take advantage of Perl's index function, something like this:

 use PDL; ... (a bunch of PDL lines of code) ...; $tmpsub = substr( $perlstr , index( $perlstr , /myregex/ ) , $position );

The problem with the above line is that it attempts to use PDL::Index. I have tried to the following as a solution:

 use PDL; ... (a bunch of PDL lines of code) ...; $tmpsub = substr( $perlstr , $main::index( $perlstr , /myregex/ ) , $position );

But this fails because of my lack of understanding. Basically I would like to use Perl's main 'index' function in the above line and not PDL's 'index'. Hopefully this is a straightforward solution, and that I will not receive too much bludgeoning for having asked such a trivial question. Kind Regards, Dan

Comment on perlfunc index name conflict
Select or Download Code
Re: perlfunc index name conflict
by Anonymous Monk on Sep 23, 2012 at 22:40 UTC
    Well, CORE::index, but FWIW, I doubt PDL exports an index function
      I doubt PDL exports an index function

      I expected the same ... but, apparently, not so:
      use warnings; use strict; use warnings; use PDL; print index('hello world', 'lo w'), "\n";
      This outputs:
      Argument "hello world" isn't numeric in subroutine entry at try.pl lin +e 7. Argument "lo w" isn't numeric in subroutine entry at try.pl line 7. 0
      If we comment out the "use PDL;" we then get the expected output of "3".
      As Anonymous Monk said, one solution is to use CORE::index() instead of index().
      Another solution is to use PDL(); instead of use PDL;

      But it's a bit rude of PDL to be exporting this function by default, isn't it ?

      Update: The PDL documentation states:
      PDL includes about a dozen perl modules that form the core of the language, plus additional modules that add further functionality. The perl module "PDL" loads all of the core modules automatically, making their functions available in the current perl namespace.
      and
      Exports "use PDL;" exports a large number of routines into the calling namespace. If you want to avoid namespace pollution, you must instead "use PDL::Lite", and include any additional modules explicitly.
      So this *is* documented ... and it seems that the recommended solution is to "use PDL::Lite;".

      Cheers,
      Rob
Re: perlfunc index name conflict
by chuckbutler (Vicar) on Sep 23, 2012 at 22:50 UTC

    If you would like to use supplied Perl index function, qualify it CORE::index.

    Also, the second parameter should be a substring to match, not a regular expression. See index.

    Good luck. -c

      the second parameter should be a substring to match, not a regular expression

      Well, /(\w+)/ will return a substring

        In scalar context such as supplied by index, a successfully matching regex will return 1, which will be stringized to '1' for the substring for which to search:

        >perl -wMstrict -le "my $str = 'abc1def'; $_ = 'xyz'; ;; print 'index: ', index $str, /(\w+)/; print qq{captured: '$1'}; " index: 3 captured: 'xyz'

        An unsuccessful regex match returns the empty string in scalar context.

      Thank you CORE::index has solved my problem.
Re: perlfunc index name conflict
by ikegami (Pope) on Sep 24, 2012 at 01:10 UTC
    BEGIN { delete $main::{index} }
    will "unimport" index from main so you don't have to use CORE:: everywhere.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://995263]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2014-11-27 21:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (188 votes), past polls